Strpos logic errors that are common in php
Let's take a look at the usage of strpos:
(PHP 4, PHP 5, PHP 7)
Strpos: Locate the first occurrence of a string
Mixed strpos (string $ haystack, mixed $ needle [, int $ offset = 0])
Returns the position of the needle that exists at the start of the haystack string (independent of offset ). Note that the string position starts from 0, not from 1.
If no needle is foundFALSE. (Note the returned values in this case)
2. instance description (error code that causes the problem)
It is easy for beginners to judge the true value of if. because the function does not find the corresponding character, the returned value is false, therefore, this kind of judgment may occur to deal with the problems found and not found. The result of this example shows that the character 'T' actually exists and is also found, and the position value of the character is returned, that is, 0, because 0 is a false value, the program executes the code in else, causing inconsistency between the program running result and the expected result, resulting in a so-called logical error.
3. correct code (solution)