Home > Backend Development > PHP Tutorial > Why Does Checking `strpos()` with `!== false` Fail for Strings Starting at Position Zero?

Why Does Checking `strpos()` with `!== false` Fail for Strings Starting at Position Zero?

Linda Hamilton
Release: 2024-12-16 10:42:12
Original
348 people have browsed it

Why Does Checking `strpos()` with `!== false` Fail for Strings Starting at Position Zero?

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template