PHP strrchr() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
String

Definition

In PHP, strrchr() finds the last occurrence of a character and returns the rest of the string from that point.

It is a quick way to get a file extension or the last part of a path.

Syntax

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

PHP
$file = strrchr("/var/www/index.php", "/");
CallWhat it does
strrchr("/var/www/index.php", "/")Returns "/index.php".
strrchr("archive.tar.gz", ".")Returns ".gz".
strrchr("hello", "z")Returns false because there is no match.

If the needle has more than one character, only the first character is used. In PHP 8.3 and later, an optional third argument $before_needle can return the part before the match.

Library declarations
PHP
strrchr(string $haystack, string $needle): string|false

Parameters

ParameterDescription
haystackRequired. The string to search in.
needleRequired. The character to look for. Only its first character is used.

Return Value

Return ValueDescription
stringThe part of the string from the last match to the end, including the matched character.
falseReturned when the character is not found.

PHP Examples

Example 1 — Get the Last Part of a Path

Find the final slash and take everything after it.

PHP
<?php
$path = "/var/www/index.php";
$file = strrchr($path, "/");

echo $file, PHP_EOL;

How It Works

  1. Store a file path.
  2. Search for the last "/".
  3. strrchr returns the text starting at that slash.
  4. The result still includes the slash itself.

Example 2 — Get a File Extension

With several dots, only the last one matters.

PHP
<?php
$name = "archive.tar.gz";

echo strrchr($name, "."), PHP_EOL;

How It Works

  1. The name contains two dots.
  2. strrchr searches from the end of the string.
  3. It stops at the last dot.
  4. The result is ". gz".

Example 3 — Handle a Missing Character

When there is no match, you get false.

PHP
<?php
$result = strrchr("hello", "z");

var_dump($result);

How It Works

  1. Search "hello" for the letter "z".
  2. The letter does not exist.
  3. strrchr returns false.
  4. Always check with === false before using the result.

Frequently Asked Questions

strrchr() finds the last occurrence of a character in a string and returns the substring from that character to the end.
strstr() finds the first match of a whole substring. strrchr() finds the last match of a single character.
Only the first character of the needle is used. If you need to find a whole word, use strrpos() with substr().
Use strrchr($filename, '.') and then substr(..., 1) to remove the dot. For file paths, pathinfo($filename, PATHINFO_EXTENSION) is often better.
It returns false. Check the result with === false.

Next: strrev()

strrev() reverses a string character by character.

strrev() 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