PHP strpos() Function
PHP strpos() Syntax
strpos(string $haystack, string $needle, int $offset = 0): int|false
PHP strpos() Basic examples
$text = "Hello world";
$pos = strpos($text, "world");
var_dump($pos);
int(6)
Searches for the substring "world" and returns its starting position.
$value = "abc";
$pos = strpos($value, "a");
var_dump($pos);
int(0)
When the substring is found at the beginning, strpos() returns 0.
$result = strpos("example", "z");
var_dump($result);
bool(false)
If the substring is not found, strpos() returns false.
PHP strpos() Real-world usage
$email = "user@example.com";
if (strpos($email, "@") !== false) {
echo "Valid email format";
}
Valid email format
Checks whether an email-like string contains the @ character.
$path = "/api/v1/users";
if (strpos($path, "/api/") === 0) {
echo "API request";
}
API request
Detects whether a URL path starts with a specific prefix.
$config = "mode=production";
$pos = strpos($config, "=");
$value = substr($config, $pos + 1);
echo $value;
production
Extracts a configuration value using strpos() and substr().
PHP strpos() Edge cases
$result = strpos("", "test");
var_dump($result);
bool(false)
Searching in an empty string always returns false.
$result = strpos("hello", "", 0);
var_dump($result);
int(0)
An empty substring is considered found at position 0.
PHP strpos() Common mistakes
Using loose comparison with the return value
Treating the return value of strpos() as a boolean causes bugs when the position is 0.
if (strpos("abc", "a")) {
echo "Found";
}
if (strpos("abc", "a") !== false) {
echo "Found";
}
Always use strict comparison (!== false).
Negating the return value directly
Negating the return value causes incorrect logic when the match is at position 0.
if (!strpos("test", "e")) {
echo "Not found";
}
if (strpos("test", "e") === false) {
echo "Not found";
}
Explicitly compare the return value against false.
PHP strpos() Frequently Asked Questions
What does strpos() do in PHP?
strpos() finds the position of the first occurrence of a substring inside a string and returns its numeric position.
What does strpos() return if the string is not found?
strpos() returns false if the substring is not found in the string.
Why is strpos() returning 0?
strpos() returns 0 when the substring is found at the beginning of the string. This is a valid result and not an error.
How to correctly check if strpos() found a value?
Use strict comparison: if (strpos($string, $search) !== false) to correctly detect matches.
Is strpos() case-sensitive?
Yes, strpos() is case-sensitive. Use stripos() for case-insensitive searches.
What is the difference between strpos() and stripos()?
strpos() is case-sensitive, while stripos() performs a case-insensitive search.
Can strpos() search from a specific position?
Yes, you can provide an offset as the third argument to start searching from a specific position in the string.
Does strpos() work with multibyte strings?
No, strpos() is not multibyte-safe. Use mb_strpos() for UTF-8 or multibyte strings.
What is the difference between strpos() and str_contains()?
strpos() returns the position of a substring, while str_contains() only returns true or false if the substring exists.
Can strpos() return negative values?
No, strpos() returns either a zero-based position or false. It never returns negative values.
How to find the last occurrence of a substring in PHP?
Use strrpos() to find the position of the last occurrence of a substring in a string.
What is a real-world use case of strpos()?
strpos() is commonly used to validate input, detect substrings like email domains, check URL patterns, or filter content.