View Full Version : get the last character of a string?

11-22-2011, 03:51 AM
I'm wondering how you can grab the last character of a string.
For example:

$string = "The quick brown fox jumps over the lazy dog";

echo $string{0};
//Echoes the letter T

echo $string{5};
//Echoes the letter u

My question is, how can you get the LAST character of the string without knowing its length? I tried using -1 but ad no luck (unless it was in another part that it came out wrong). If their isn't another way I could always use strlength and get it but I'm just wondering if theirs an actual way for this

11-22-2011, 03:55 AM
echo substr($string, -1, 1);


echo $string[strlen($string)-1];

11-22-2011, 11:27 AM
Check this manual


This will help you in future with functions