Consider the following example:
$a='This is a test';
If I now do:
if(strpos($a,'is a') !== false) {
echo 'True';
}
It get:
True
However, if I use
if(strpos($a,'is a') === true) {
echo 'True';
}
I get nothing. Why is !==false
not ===true
in this context
I checked the PHP docs on strpos()
but did not find any explanation on this.
Because
strpos()
never returns true:It only returns a boolean if the needle is not found. Otherwise it will return an integer, including -1 and 0, with the position of the occurrence of the needle.
If you had done:
You would have usually gotten expected results as any positive integer is considered a truthy value and because type juggling when you use the
==
operator that result would be true. But if the string was at the start of the string it would equate to false due to zero being return which is a falsey value.