Summary: in this tutorial, you’ll learn how to use the PHP strpos() function to get the index of the first occurrence of a substring in a string.
Introduction to the PHP strpos() function #
The PHP strpos() function returns the index of the first occurrence of a substring within a string.
Here’s the syntax of the strpos() function:
strpos ( string $haystack , string $needle , int $offset = 0 ) : int|falseThe strpos() function has the following parameters:
- The
$haystackis a string to search in. - The
$needleis a string value to search for. - The
$offsetis an integer that represents the index at which thestrpos()function starts the search. The$offsetdefaults to 0.
The $offset can be positive or negative. If $offset is positive, the strpos() function starts the search at $offset number of characters to the end of the string.
If the $offset is negative, the strpos() function starts at the $offset number of characters to the beginning of the string.
If the strpos() doesn’t find the $needle in the $haystack, it returns false.
PHP strpos() function examples #
Let’s take some examples of using the strpos() function.
1) Using PHP strpos() function to search for a substring example #
The following example uses the strpos() function to search for the substring 'to' in the string 'To do or not to do':
<?php
$str = 'To do or not to do';
$position = strpos($str, 'do');
echo $position; // 3Output:
3
2) Using PHP strpos() function to search for a substring with an offset example #
The following example uses the strpos() function to search for the substring 'do' in the string 'To do or not to do' start from the index 5:
<?php
$str = 'To do or not to do';
$position = strpos($str, 'do', 4);
echo $position;Output:
16In this example, the strpos() returns the index of the second occurrence of the 'do' substring because it starts the search at index 4.

3) Using PHP strpos() function gotchas #
The following example uses the strpos() function to search for the substring 'To' in the string 'To do or not to do':
<?php
$str = 'To do or not to do';
$position = strpos($str, 'To');
if ($position) {
echo $position;
} else {
echo 'Not found';
}Output:
Not foundThe string 'To' locates at the beginning of the string; therefore, the strpos() function returns 0. However, PHP evaluates 0 as false. So you see the 'not found' message in the output.
To avoid this issue, you should always compare the result of the strpos() function with true or false using the === or !== operator. For example:
<?php
$str = 'To do or not to do';
$position = strpos($str, 'To');
if ($position !== false) {
echo $position; // 0
} else {
echo 'Not found';
}Output:
0PHP stripos() function #
The stripos() function is like the strpos() function except that it searches for the substring case-insentively. The following example returns false:
<?php
$str = 'PHP is cool';
$position = strpos($str, 'php');
var_dump($position); // bool(false)Output:
bool(false)To search for a substring case-insensitively, you use the stripos() function:
<?php
$str = 'PHP is cool';
$position = stripos($str, 'php');
var_dump($position); // 0Summary #
- Use the
strpos()function to return the index of the first occurrence of a substring in a string. - Use the
stripos()function to search for the substring case-insensitively.
Thank you for your feedback!