PHP strrchr() finds the last occurrence of a character and returns the remaining substring. The examples below show how the call behaves, where it is useful, and which limitations matter.
What Is strrchr() in PHP?
Finds the last occurrence of a character and returns the remaining substring.
Its call structure is:
strrchr($haystack, $needle, [$before_needle])
The arguments identify the values or resources passed to strrchr(). Read the examples with attention to the type of each supplied argument and to whether the function modifies its input or returns a separate result.
Example 1: Basic Usage
This example introduces strrchr with a direct call and a small, readable input.
<?php
echo strrchr("photo.backup.jpg", ".");
Output:
.jpg
Finds the last occurrence of a character and returns the remaining substring. Only the first byte of the supplied character string is used as the needle.
Example 2: Another Behavior or Input
Compare this input with the previous example: changing the data or option can affect how strrchr behaves.
<?php
var_dump(strrchr("filename", ".") === false);
Output:
bool(true)
The changed input illustrates why output may differ with the chosen values or environment. Only the first byte of the supplied character string is used as the needle.
Practical Example: Using the Feature
The following example incorporates strrchr into a workflow. Values such as paths, connection handles and configuration options must come from trusted application settings.
<?php
$fileName = "archive.tar.gz"; $extension = strrchr($fileName, "."); if ($extension !== false) { echo substr($extension, 1); }
Output:
gz
In a real application, check each operation result, validate values before use and clean up acquired resources. Only the first byte of the supplied character string is used as the needle.
Important Behavior and Edge Cases
- Purpose: Finds the last occurrence of a character and returns the remaining substring.
- Key limitation: Only the first byte of the supplied character string is used as the needle.
- Failure handling: Check return values and handle invalid inputs and unavailable operating-system or extension features where relevant.
Comparison With Related Functions
| Function or Approach | Best Fit |
|---|---|
strrchr() | Finds the last occurrence of a character and returns the remaining substring. |
strrpos() | Returns a byte position of the final occurrence, not the suffix string. |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
| Only the first byte of the supplied character string is used as the needle. | The correct approach depends on this behavior: Only the first byte of the supplied character string is used as the needle. |
| Ignoring errors or cleanup after the operation. | Validate inputs and check each result; close files, restore settings or release resources when the operation requires it. |
Key Takeaways
- Finds the last occurrence of a character and returns the remaining substring.
- Only the first byte of the supplied character string is used as the needle.
- Use the examples as starting points, and adapt environmental paths, resources and permissions rather than assuming fixed results.
strrchr is useful when its specific behavior fits the problem. The safest implementations combine the correct API with input validation, error checking and resource cleanup.