Skip to content
PHP Logo

What is the best method to extract the last character of a string in PHP?

To extract the last character of a string in PHP, you can use the substr() function with a negative offset. By passing -1 as the second argument to substr(), you can retrieve the last character of the string. This approach is simple and effective for extracting the last character from a string in PHP.

php
$string = "Hello World";
$lastCharacter = substr($string, -1);
echo $lastCharacter; // Output: d