Why use !== FALSE to check string contains a specific word in php?

In this article, We’ll see why we use !== FALSE to check sub-string is located inside another string.

strpos — Find the position of the first occurrence of a case-sensitive substring in a string.

strpos returns the position of a string inside another. if the string is not found, it returns false and return 0 when there is a match at the beginning of the string so it’s recommended to use the identity comparison operators (===, !==), because PHP considers 0 as a “falsy” value.




The thing is we should use === instead of == to compare 0 and FALSE.

consider this example:

 // Find the position of the 'a' character in the 'abc' string:
 strpos('abc', 'a') !== false; // return true, position is 0
 strpos('abc', 'a') != false; // return false, 0 is "falsy"

Since the “a” occurs at the first position (index 0) then PHP considers “if (0)” to be false.

For more informations, see Comparison Operators :

=== and !== are strict comparison operators. !== FALSE is not the same as === TRUE since, for example, 1 !== FALSE is Ok (values and types are not equal), but 1 === True is not Ok (values are equal, but types are not).

That’s it!. Please share your thoughts or suggestions in the comments below.

Leave a Reply

Your email address will not be published. Required fields are marked *