Current Location: Home> Latest Articles> Is stripos returning 0 false? Why is there a wrong judgment?

Is stripos returning 0 false? Why is there a wrong judgment?

M66 2025-05-13

In PHP, the stripos function is used to find the position of a string in another string (case insensitive). This function returns the location index found, or if not found, false . However, when the scripos function returns 0, many developers mistakenly think it is false because in PHP, the number 0 and the boolean value false are equivalent. So, is the stripos function returning 0 false ? Why are there any mistakes in judgment? How should we correctly understand this return value? Next, we answer these questions one by one.

1. Basic usage of stripos function

The function prototype of stripos is as follows:

 stripos(string $haystack, string $needle, int $offset = 0): int|false
  • $haystack : The string to be searched for.

  • $needle : The string to be found.

  • $offset : Start the search from this location (optional).

The function of this function is to return the position where $needle first appears in $haystack (starting from 0). If the string is not found, false is returned.

Example:

 $haystack = "Hello World";
$needle = "world";

$position = stripos($haystack, $needle);

echo $position;

The output is:

 6

This is because in the string "Hello World" , the first location where "world" (case insensitive) appears is index position 6.

2. Why does stripos cause misunderstanding when returning 0 ?

Many developers may directly compare it with false when judging the return value of the stripos function. Suppose there is the following code:

 $haystack = "test string";
$needle = "test";

if (stripos($haystack, $needle) === false) {
    echo "Not found";
} else {
    echo "Found";
}

In this case, stripos returns 0 because "test" appears from position 0 in "test string" . Therefore, despite finding the matching string, the program still outputs "Not Found" because 0 is considered false in PHP.

3. How to correctly judge the return value of stripos ?

In order to correctly judge the return value of stripos , we need to avoid directly comparing with false . The correct approach is to use the strict comparison operator === to determine whether false is returned, and consider that 0 is a valid matching position. Here is a modified code example:

 $haystack = "test string";
$needle = "test";

$position = stripos($haystack, $needle);

if ($position === false) {
    echo "Not found";
} else {
    echo "Found,Location is:".$position;
}

In this case, the program will output correctly:

 Found,Location is:0

In this way, through the strict comparison operator === , we can distinguish between false (indicating that no found) and 0 (indicating that the matching position is 0).

4. How to avoid making mistakes in judgment?

When judging the return value of stripos , be sure to pay attention to the following points:

  • Do not use == comparison : because in PHP, 0 and false are equivalent, using == will lead to incorrect judgments.

  • Use strict comparison === : This can distinguish between false (not found) and 0 (indicates that the string has a match at the beginning).

In addition, it is recommended to set reasonable default values ​​or perform error handling for the values ​​returned by stripos in actual development to ensure the robustness of the program. For example:

 $position = stripos($haystack, $needle);

if ($position === false) {
    // Handle unfound situations
} else {
    // Handle the found situation
}

5. Summary

In PHP, when the stripos function returns 0 , it does not mean that the matching string cannot be found, but rather that the matching string starts at position 0 . In order to avoid mistakenly considering 0 to be false , we should use the strict comparison operator === to determine whether the return value is false , thereby ensuring that the program's judgment logic is correct.

By understanding the return value of stripos and taking appropriate comparison methods, we can effectively avoid judgment errors and write more robust PHP code.