phpstrpos

Why is strpos('L', 'L') === true evaluating to false?


I am having problems with strpos evaluating properly.

$status = "L";
$x = strpos($status,'L');
echo var_export($x,true);
echo "<br/>";
if (strpos($status,'L') === true) {echo "L is there!.";}
else {echo "No L Found!";}

This outputs:

0
No L Found!

With how I understand strpos and the "===" vs the "==" this should be finding the L.

What do I not understand?


Solution

  • strpos doesn't return true, it returns false if the string isn't found or the index if it is found.

    From the official docs:

    Returns the position of where the needle exists relative to the beginning of the haystack string (independent of offset). Also note that string positions start at 0, and not 1.

    Returns FALSE if the needle was not found.