Syntax
The syntax of the strpos() function is:
int|false strpos(string $haystack, string $needle, int $offset = 0)
- $haystack: The main string in which the search will be performed.
- $needle: The substring you are searching for in the main string.
- $offset (optional): The position in the string from which the search should begin. By default, it starts from the beginning (index 0).
Let’s take an example:
$string = "Hello, welcome to the world of PHP!";
$substring = "welcome";
$position = strpos($string, $substring);
echo $position;

You can also try this code with Online PHP Compiler
Run Code
Output:
7
In this example, `strpos` searches for the substring `"welcome"` in the string `"Hello, welcome to the world of PHP!"`. It finds the substring starting at position 7 (remember, string positions start from 0).
If the substring is not found, `strpos` returns `false`. For example:
$string = "Hello, welcome to the world of PHP!";
$substring = "coding";
$position = strpos($string, $substring);
if ($position === false) {
echo "Substring not found!";
} else {
echo "Substring found at position: " . $position;
}

You can also try this code with Online PHP Compiler
Run Code
Output:
Substring not found!
Notice the use of `===` (strict comparison) instead of `==`. This is because `strpos` can return `0` if the substring is found at the beginning of the string, and `0` is considered `false` in a loose comparison. Using `===` ensures accurate results.
Parameter Values
- $haystack: This is the main string where the function will look for the substring. It is a required parameter.
- $needle: This is the substring that you want to find in the main string. It is also required.
- $offset: This is an optional parameter. It sets the starting position for the search. If not provided, the search begins at the start of the string.
Return Type
The strpos() function returns:
- Integer: If the substring (needle) is found, it returns the position of its first occurrence (0-based index).
- false: If the substring is not found in the main string, the function returns false.
Note: Be cautious when checking the return value, as strpos() may return 0, which is a valid position. To handle this correctly, use the strict comparison (===) operator.
Examples
Example 1: Basic Usage
<?php
$text = "Learn PHP with Coding Ninjas";
$search = "PHP";
$position = strpos($text, $search);
if ($position !== false) {
echo "The substring '$search' was found at position $position.";
} else {
echo "The substring '$search' was not found.";
}
?>

You can also try this code with Online PHP Compiler
Run Code
Output:
The substring 'PHP' was found at position 6.
Explanation:
In the string "Learn PHP with Coding Ninjas", the substring "PHP" starts at the 6th index (0-based).
Example 2: Case Sensitivity
<?php
$text = "Learn PHP with Coding Ninjas";
$search = "php";
$position = strpos($text, $search);
if ($position !== false) {
echo "The substring '$search' was found at position $position.";
} else {
echo "The substring '$search' was not found.";
}
?>

You can also try this code with Online PHP Compiler
Run Code
Output:
The substring 'php' was not found.
Explanation:
Since strpos() is case-sensitive, it does not find "php" because the string contains "PHP" with uppercase letters.
Example 3: Using the Offset Parameter
<?php
$text = "PHP is fun. Learn PHP with Coding Ninjas.";
$search = "PHP";
$position = strpos($text, $search, 10);
if ($position !== false) {
echo "The substring '$search' was found at position $position.";
} else {
echo "The substring '$search' was not found.";
}
?>

You can also try this code with Online PHP Compiler
Run Code
Output:
The substring 'PHP' was found at position 21.
Explanation:
The offset parameter starts the search from index 10. The second occurrence of "PHP" starts at index 21.
Example 4: Handling false Return Value
<?php
$text = "Coding Ninjas offers PHP tutorials.";
$search = "Java";
$position = strpos($text, $search);
if ($position === false) {
echo "The substring '$search' was not found in the text.";
} else {
echo "The substring '$search' was found at position $position.";
}
?>

You can also try this code with Online PHP Compiler
Run Code
Output:
The substring 'Java' was not found in the text.
Explanation:
The substring "Java" is not present in the main string, so the function returns false.
Common Use Cases
- Validating Input: Check if a particular word exists in user input.
- Parsing URLs: Find specific elements in URLs (e.g., detecting a query parameter).
- String Manipulation: Locate and replace parts of a string dynamically.
Frequently Asked Questions
What happens if the substring is not found in the string?
If the substring is not found, the strpos() function returns false. It is important to use strict comparison (===) to differentiate between false and 0.
Can strpos() handle multibyte characters?
No, strpos() does not handle multibyte characters like those in UTF-8. For multibyte strings, use the mb_strpos() function.
Is strpos() case-sensitive?
Yes, strpos() is case-sensitive. To perform a case-insensitive search, use the stripos() function.
Conclusion
In this article, we discussed the PHP strpos() function in detail. We learned its syntax, parameters, return types, and practical examples to search for substrings within a string. The strpos() function is a versatile tool for string handling, useful in various scenarios like input validation, parsing, and string manipulation.
You can also check out our other blogs on Code360.