find the last character in a string in php

In PHP, you can use the strlen() function to find the length of a string and then access the last character using indexing. Here's an example:

main.php
$string = "Hello World!";
$length = strlen($string);
$lastChar = $string[$length - 1];
echo $lastChar; // Output: !
116 chars
5 lines

In this example, we first get the length of the string using strlen() and store it in the $length variable. We then access the last character of the string using indexing ($string[$length - 1]) and store it in the $lastChar variable. Finally, we output the last character using the echo statement.

related categories

gistlibby LogSnag