0

So I have this code :

$str1 = 'yabadabadoo';
$str2 = 'yaba';
if (strpos($str1,$str2)) {
    echo "\"" . $str1 . "\" contains \"" . $str2 . "\"";
} else {
    echo "\"" . $str1 . "\" does not contain \"" . $str2 . "\"";
}

Which gives me "yabadabadoo" does not contain "yaba". Why? Shouldn't it give me the other result?

1 Answers1

1

Because the string is at index 0. From the official documentation

Warning This function may return Boolean FALSE, but may also return a non-Boolean value which evaluates to FALSE. Please read the section on Booleans for more information. Use the === operator for testing the return value of this function.

so you should change your if statement

Lorenzo S
  • 1,397
  • 13
  • 25