How to get the last char of a string in PHP?

Issue

I need to get the last character of a string.
Say I have “testers” as input string and I want the result to be “s”. how can I do that in PHP?

Solution

substr("testers", -1); // returns "s"

Or, for multibyte strings :

mb_substr("multibyte string…", -1); // returns "…"

Answered By – Rich Adams

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published