strrpos() Function in PHP 8.2, PHP 8.3 & PHP 8.4

The `strrpos()` function in PHP finds the position of the last occurrence of a substring in a string. It's case-sensitive and returns the position as an integer, PHP 8.PHP 8.1,PHP 8.2,PHP 8.3 and PHP 8.4.
Syntax<?phpstrrpos(string $haystack, string $needle, int $offset = 0): int|false?>
Parameters`$haystack`: The string to search in`$needle`: The substring to search for`$offset` (optional): If specified, search will start this number of characters from the beginning
Example in PHP 8.2<?php$string = "Hello World! Hello PHP! Hello World Again!";// Find last position of "Hello"$lastPos = strrpos($string, "Hello");echo "Last 'Hello' at position: " . $lastPos . "\n"; // Output: 24// Find last position of "World"$lastWorld = strrpos($string, "World");echo "Last 'World' at position: " . $lastWorld . "\n"; // Output: 30// Case-sensitive search (won't find lowercase)$lastHelloLower = strrpos($string, "hello");var_dump($lastHelloLower); // Output: bool(false)// Search with offset (only searches from position 0 to 10)$lastHelloWithOffset = strrpos($string, "Hello", 10);echo "Last 'Hello' before position 10: " . $lastHelloWithOffset . "\n"; // Output: 0// Searching for a single character$lastSpace = strrpos($string, " ");echo "Last space at position: " . $lastSpace . "\n"; // Output: 36// Practical example - getting file extension$filename = "document.backup.pdf";$lastDot = strrpos($filename, ".");if ($lastDot !== false) {$extension = substr($filename, $lastDot + 1);echo "File extension: " . $extension . "\n"; // Output: pdf}?>
Important Notes for PHP 8.21. The function is still binary-safe (handles all characters including null bytes)2. The behavior remains consistent with previous PHP versions3. Remember that string positions start at 0, not 14. Always use strict comparison (`===` or `!==`) when checking the result, as `0` (found at position 0) and `false` (not found) are different but loosely equalWhen to Use `strrpos()`Finding the last occurrence of a substringParsing strings where the last occurrence is significant (like file extensions)When you need case-sensitive search.