I'm trying to get this script to work.
The idea is that if the input string ($query) doesn't start with '/t' AND contains one of the $trigger words, an $error is set.
I can't get this to work and I'm not sure why.
<?php
    $error = false;
    $triggers = array('sell', 'buy', 'trade', 'trading');
    $query = 'buying stuff';
    if (!empty($query)) {
        if (substr($query, 0, 2开发者_开发百科) != '/t') {
            foreach ($triggers as $trigger) {
                if (strpos($query, $trigger)) {
                    $error = true;
                }
            }
        }
    }
    if ($error) {
        echo "fail";
    }
    else {
        echo "pass";
    }
?>
That should have triggered the error but it doesn't seem to be. What am I doing wrong?
If the function strpos fails to find the string it returns false. Also note that if the search string is found at the very beginning a 0 is returned.
Change
if (strpos($query, $trigger)) {
to
if (strpos($query, $trigger) !== false) {
change this to
if (strpos($query, $trigger) !==false ) {
checkout how strpos works
here is the problem :
  if (strpos($query, $trigger)) {
This evaluates to 0 if string is found at index 0, which causes IF statement to be false so use
(strpos($query,$trigger) !== false )
<?php
    $error = false;
    $triggers = array('sell', 'buy', 'trade', 'trading');
    $query = 'buying stuff';
    if (!empty($query)) {
        if (substr($query, 0, 2) != '/t') {
            foreach ($triggers as $trigger) {
                if (strpos($query, $trigger) !== false) {
                    $error = true;
                    break;
                }
            }
        }
    }
    if ($error) {
        echo "fail";
    }
    else {
        echo "pass";
    }
?>
Please use the "break" keyword, along with the proper checking.
Hope it helps.
 
         
                                         
                                         
                                         
                                        ![Interactive visualization of a graph in python [closed]](https://www.devze.com/res/2023/04-10/09/92d32fe8c0d22fb96bd6f6e8b7d1f457.gif) 
                                         
                                         
                                         
                                         加载中,请稍侯......
 加载中,请稍侯......
      
精彩评论