Loosely Checking strpos() Return Value Yields Unintended Results
In attempting to locate string occurrences within another string, you may encounter discrepancies when utilizing the strpos() function. The intended behavior of checking if two strings are present and ensuring the first precedes the second using !== false can yield unexpected outcomes.
As stated in the PHP manual, strpos() returns false when a string is not found. However, when a string commences at position zero (resulting in a strpos() return value of 0), the statement strpos($grafik['data'], $ss1) !== false may be evaluated as false.
To rectify this, replace the !== false with === 0 in your code. This modification ensures that the statement evaluates to true when strpos() returns 0, indicating that the string is present at the beginning of the designated string.
The above is the detailed content of Why Does Checking `strpos()` with `!== false` Fail for Strings Starting at Position Zero?. For more information, please follow other related articles on the PHP Chinese website!