0

See the following issue:

$str = "video-23984"; // returns false
$str = " video-23984"; // returns true
$search= "video";

if(strpos($str,$search)) {
  echo "True";
}else {
  echo "False";
}

Why in the world does $str = "video-23984" return false? And what can I do to make it return true?

Henrik Petterson
  • 6,862
  • 20
  • 71
  • 155
  • 1
    `is_int(strpos(…))` if you're looking for the boolean test, otherwise it returns bools **or** integers (where `0` is the likely outcome). – mario Dec 28 '15 at 22:59
  • 1
    what the hell mario, this isn't a duplicate. The other question is different, even tho it's related to the same function and it's return. – Phiter Dec 28 '15 at 23:03
  • Big warning from the [PHP Docs](http://php.net/manual/en/function.strpos.php) - `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.` – Mark Baker Dec 28 '15 at 23:14

1 Answers1

0

In the first string the word video is in the first position, which means 0.

In the second it's in the second position, which means 1. Since you're returning it into an if, the 0 is a boolean for false. That's why you're getting false as a result.

Phiter
  • 14,570
  • 14
  • 50
  • 84