How to Get the Last Character of a String in PHP: A Complete Guide for Beginners and Pros
\n\nImagine this: you're writing a script that needs to check if an email ends with @gmail.com, or you need to extract the last letter from a username. It seems like a simple task — get the last character of a string in PHP. But it's not that straightforward! Even in such a simple question, there are many pitfalls, especially when dealing with Russian, emojis, or special characters.
According to Stack Overflow, the question "How to get the last char of a string in PHP?" gets thousands of views every month. And for good reason: beginners often stumble over substr() with negative indices, while experienced developers debate performance and correct handling of UTF-8.
In this article, we'll cover 5 working methods, show real code examples, and give clear recommendations. Let's go!
\n\nMethod #1: The Classic — substr() with a Negative Index
\n\nThe most popular and concise method. The substr() function accepts a negative length value to count characters from the end of the string.
<?php\n$string = "Hello, World!";\n$lastChar = substr($string, -1);\necho $lastChar; // Outputs: !\n?>\n\nPros:
\n- \n
- Short and clear \n
- Fast performance \n
Cons:
\n- \n
- Does not support multibyte encodings (Cyrillic, CJK characters) \n
- Can return a "broken" character for UTF-8 strings \n
Example of the problem with Cyrillic:
\n\n<?php\n$string = "Привет";\n$lastChar = substr($string, -1);\necho $lastChar; // Outputs: � (question mark in a diamond)\n?>\n\nWhy does this happen? Because Russian letters take up 2 bytes in UTF-8, and substr() works with bytes, not characters. For English text, this method is ideal, but for multilingual projects — it's not.
Method #2: The Correct One — mb_substr() for Multibyte Strings
\n\nIf your project works with Unicode (which is practically mandatory today), use mb_substr() from the mbstring extension.
<?php\n$string = "Привет, мир!";\n$lastChar = mb_substr($string, -1);\necho $lastChar; // Outputs: !\n?>\n\nNow for the fun part — working with emojis:
\n\n<?php\n$string = "👋 Привет 🌍";\n$lastChar = mb_substr($string, -1);\necho $lastChar; // Outputs: 🌍\n?>\n\nPros:
\n- \n
- Correctly handles any encoding (UTF-8, UTF-16, etc.) \n
- Works with emojis and special characters \n
Cons:
\n- \n
- Requires the mbstring extension to be installed (available on almost all hosting providers) \n
- Slightly slower than
substr()\n
Method #3: An Alternative — str_split() and end()
\n\nYou can split the string into an array of characters and take the last element. It looks elegant, but there's a catch.
\n\n<?php\n$string = "PHP";\n$chars = str_split($string);\n$lastChar = end($chars);\necho $lastChar; // Outputs: P\n?>\n\nNote: str_split() also doesn't work well with multibyte strings. For Cyrillic, you need to use preg_split() with the Unicode flag:
<?php\n$string = "Код";\n$chars = preg_split('//u', $string, -1, PREG_SPLIT_NO_EMPTY);\n$lastChar = end($chars);\necho $lastChar; // Outputs: д\n?>\n\nPros:
\n- \n
- Intuitive code \n
- You can easily get not only the last character but any character \n
Cons:
\n- \n
- Excessive memory usage (creates an array) \n
- Slower than direct functions \n