PHP strpos() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
String

Definition

In PHP, strpos() finds the position of the first occurrence of a substring inside a string.

The search is case-sensitive and counting starts at 0. If the text is not found, it returns false.

Syntax

Call strpos() with the values you need. Start with this line.

PHP
$pos = strpos("Hello World", "World");
CallWhat it does
strpos("Hello World", "World")Returns 6.
strpos("a-b-c", "-", 2)Returns 3 (search starts at index 2).
strpos("apple", "z")Returns false because there is no match.

For a simple yes or no check on PHP 8.0 and later, str_contains() is clearer. For case-insensitive search, use stripos().

Library declarations
PHP
strpos(string $haystack, string $needle, int $offset = 0): int|false

Parameters

ParameterDescription
haystackRequired. The string to search in.
needleRequired. The text to look for. An empty needle is allowed in PHP 8 and returns 0.
offsetOptional. Where to start searching. A negative value counts from the end of the string.

Return Value

Return ValueDescription
intThe zero-based position of the first match.
falseReturned when the needle is not found.

PHP Examples

Example 1 — Find a Word in a Sentence

The simplest use: get the index where a word starts.

PHP
<?php
$text = "Hello World";
$pos = strpos($text, "World");

echo $pos, PHP_EOL;

How It Works

  1. Store a sentence in a variable.
  2. Search for "World".
  3. Counting from 0, the "W" is at index 6.
  4. echo prints the position.

Example 2 — Position 0 Is Not false

A match at the start returns 0. Compare with !== false.

PHP
<?php
$pos = strpos("apple", "a");

if ($pos !== false) {
    echo "Found at ", $pos, PHP_EOL;
}

How It Works

  1. Search for "a" in "apple".
  2. The letter is the first character, so strpos returns 0.
  3. A loose check like if ($pos) would treat 0 as false.
  4. Using !== false correctly detects the match.

Example 3 — Start Searching from an Offset

Skip the first part of the string with the third argument.

PHP
<?php
$text = "a-b-c";

echo strpos($text, "-"), PHP_EOL;
echo strpos($text, "-", 2), PHP_EOL;

How It Works

  1. The string has a dash at index 1 and another at index 3.
  2. The first call starts at 0 and finds index 1.
  3. The second call starts at index 2, so it skips the first dash.
  4. It returns 3.

Frequently Asked Questions

strpos() returns the index of the first occurrence of a substring in a string, starting at 0. It returns false if the text is not found.
0 means the match is at the very start. In an if statement 0 is falsy, so use $pos !== false to check for a match.
Yes. Use stripos() for a case-insensitive search.
Use str_contains($haystack, $needle). It returns true or false and avoids the 0 versus false problem.
Use strrpos() for the last match, or strripos() if you want to ignore case.

Next: strrchr()

strrchr() finds the last occurrence of a character and returns the rest of the string from.

strrchr() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful