Home >Backend Development >PHP Tutorial >Why Use `!==` Instead of `>` When Checking `strpos()` Return Values in PHP?
` When Checking `strpos()` Return Values in PHP? " />
Unintended Result from Loosely Checking strpos() Return Value
strpos() is a PHP function used to search for the first occurrence of a substring within a string. While the PHP manual states that strpos() returns false if the string is not found, it is important to consider how PHP handles different data types.
In PHP, the value 0 evaluates to false in a boolean context. This can lead to unexpected results when checking the return value of strpos(). If your string starts at position zero, strpos() will return 0, which may result in a false comparison when using the == operator.
To avoid this issue, it is recommended to use the === operator for testing the return value of strpos(). The === operator performs a strict equality check, ensuring that the data types of the compared values are also the same.
For instance, instead of using the following:
if (strpos($grafik['data'], $ss1) > false)
Use the following:
if (strpos($grafik['data'], $ss1) !== false)
By using !==, you ensure that strpos() returns a true boolean value (true or false) instead of evaluating the return value as zero (false).
The above is the detailed content of Why Use `!==` Instead of `>` When Checking `strpos()` Return Values in PHP?. For more information, please follow other related articles on the PHP Chinese website!