Get the last character of a string in PHP.

To obtain the last character of a string, you can use the following code:

$str = "Hello World";
$lastChar = $str[strlen($str) - 1];
echo $lastChar; // 输出 "d"

In the code above, strlen($str) is used to get the length of the string, then -1 is used to retrieve the index of the last character. Finally, $str[index] is used to get the last character in the string.

bannerAds