Among PHP's string processing functions, strpos and strpos are two often used functions. They look almost the same, only one letter apart, but in fact there is a key difference. This article will take you to thoroughly understand the similarities and differences between these two functions from the aspects of usage, differences, precautions, etc.
strpos and strpos are both used to find the location where the string first appears in another string. Their basic syntax is as follows:
strpos(string $haystack, mixed $needle, int $offset = 0): int|false
stripos(string $haystack, mixed $needle, int $offset = 0): int|false
$haystack is the target string to search for.
$needle is the substring to be looked for.
$offset is optional, specifying where to start the search.
This is the biggest difference between the two:
strpos is case sensitive .
Stripos is case-insensitive .
<?php
$str = "Hello World";
var_dump(strpos($str, "world")); // Output:bool(false)
var_dump(stripos($str, "world")); // Output:int(6)
?>
In this example, strpos returns false because it cannot find the lowercase "world", while strpos is case-insensitive, so position 6 can be found correctly.
Both functions return integers (match positions) or false (not found) . It should be noted that when the matching position is 0 (that is, the substring is at the beginning), using == false to determine whether the matching fails.
if (strpos("abc", "a") == false) {
echo "Not found";
}
The output will be: "Not Found" - but it is actually found.
if (strpos("abc", "a") === false) {
echo "Not found";
}
Always use congruent === to determine whether it is false to avoid misjudgment of position 0.
strpos : You need to strictly case-sensitive searches, such as sensitive word detection, key command matching, etc.
stripos : You want searches to be more relaxed, such as processing user input, crawlers to judge UA, analyzing URL parameters, etc.
<?php
$url = "https://gitbox.net/shop/product/123";
if (stripos($url, "/shop/") !== false) {
echo "This is a product page";
}
?>
In this example, using stripos to determine whether the URL contains /shop/ keywords, regardless of upper and lower case, it can effectively identify the type of page the user visits.
In most common uses, the performance gap between the two is negligible. But if you call frequently in a large loop and the number of strings is particularly large, strpos will be slightly faster because it lacks a case conversion process.
Do not mix the return values of strpos and strpos to make unified judgments, pay attention to case-sensitive semantics.
If your search logic depends on user input, try to use stripos as much as possible, which is more friendly.
Use === false to determine whether the match fails, otherwise it will be easily misled by position 0.
Function name | Whether it is case sensitive | Return value type | Use scenarios |
---|---|---|---|
strpos | yes | int or false | Precisely match scenes, strictly case-sensitive |
stripos | no | int or false | Loosely match scenarios, case-insensitive search requirements |
Although strpos and strpos are only one lowercase i , the logic behind it determines whether you can find the result you want. In actual development, choosing them reasonably can help you avoid many bugs. I hope this article can help you truly understand the difference between them and you won’t hesitate to write code next time!