No they're not equivalent:
strpos() returns either boolean FALSE
(if not found) or an integer offset value (which can be 0
if found at offset 0 and so on), but it never returns a boolean TRUE
. ie., Boolean TRUE
!== an INT
.
Question
Question, how come the following executed the echo:
$str = "Hello World";
if (strpos($str, 'He') !== false) {
echo 'GOOD';
}
But this doesn't:
$str = "Hello World";
if (strpos($str, 'He') === true) {
echo 'GOOD';
}
Aren't the two conditions equivalent in that they are both checking for the returned to be a boolean that is set to true? Isn't !== false the same as === true, and if not, why not?
I appreciate the clarification.
Solution
No they're not equivalent:
strpos() returns either boolean FALSE
(if not found) or an integer offset value (which can be 0
if found at offset 0 and so on), but it never returns a boolean TRUE
. ie., Boolean TRUE
!== an INT
.
OTHER TIPS
The operator ===
compares not only value but also a datatype. If strpos
finds the substring, it returns the position which is of type int
. As it is not bool, the condition is not met.