By Alvin Alexander. Last updated: June 4, 2016
PHP string FAQ: Can you share some source code for a PHP last character function (string function)?
Sure, I just developed a PHP last character function for an app I'm working on, and here's the source code for that function, aptly named last_char:
# a php last character function function last_char($str) { $len = strlen($str); return char_at($str, $len-1); }
As you can tell from the code, it returns the last character from a PHP string.
Oops, I see it requires my PHP char_at function. Here's the source code for it as well:
function char_at($str, $pos) { return $str{$pos}; }
Of course if you're a PHP whiz you can knock out this PHP last character functionality in just one or two lines of code, but I happen to like a lot of small functions that do one thing, hence this breakdown.
This PHP last character function isn't too amazing or anything like this, but hopefully it is useful. :)