PHP strrchr is an inbuilt function in PHP. It finds the last occurrence of a character in a string and returns all characters till the end of the string. In this article, we will discuss PHP strrchr Function. Also, we will discuss a few examples of using it.
You can also read about PHP strstr Function. It finds the first occurrence of a string inside another string.
Syntax
strrchr($haystack, $needle)
Parameters
The PHP strrchr Function expects two parameters. Also, both parameters are mandatory. The description of the parameters is as follows:
- $haystack: The first parameter is the input string in which we want to search in.
- $needle: The second parameter is the character we want to search for. However, if the needle consists of more than one characters, then the function uses only the first character.
Return Value
The PHP strrchr Function returns the portion of the haystack beginning from the last occurrence of the needle up to the end of haystack. However, if the needle does not exist in the haystack, it returns false.

Examples
Let’s discuss a few examples of using the strrchr function in PHP.
Example 1: Basic Usage
<?php // Input string $haystack = "Hello. Welcome|to |Concatly!"; // key to be searched $needle = "|"; echo strrchr($haystack, $needle); ?>
For instance, in the above example, we search for a “|” character in the string. The needle occurs more than once in the string. The function returns the portion of the string starting from the last occurrence of the needle.
OUTPUT: |Concatly!
Example 2: Needle Contains More than One Character
However, if the needle contains more than one character, the function only uses the first character of the needle.
<?php // Input string $haystack = "Hello. Welcome|to |Concatly!"; // key to be searched $needle = "|random"; //Only | used for search echo strrchr($haystack, $needle); ?>
In the above example, only “|” is used to search in the haystack. Again, the function returns the portion of the string starting from the last occurrence of the first character of the needle.
OUTPUT: |Concatly!
Example 3: Needle Not Found in Haystack
If the needle does not exist in the haystack, the PHP strrchr function returns false.
<?php // Input string $haystack = "Hello. Welcome|to |Concatly!"; // key to be searched $needle = "**"; var_dump(strrchr($haystack, $needle)); ?>
OUTPUT: bool(false)
Conclusion
In conclusion, we discussed the PHP strrchr Function. You can read more about it on the Official PHP Documentation. Additionally, you can learn about more PHP String Functions on Concatly.

Vishesh is currently working as a Lead Software Engineer at Naukri.com. He passed out of Delhi College of Engineering in 2016 and likes to play Foosball. He loves traveling and is an exercise freak. His expertise includes Java, PHP, Python, Databases, Design and Architecture.