How to Get the Last Character of a String in PHP: 5 Proven Methods

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

How to Get the Last Character of a String in PHP: A Complete Guide for Beginners and Pros

\n\n

Imagine 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.

\n\n

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.

\n\n

In this article, we'll cover 5 working methods, show real code examples, and give clear recommendations. Let's go!

\n\n

Method #1: The Classic — substr() with a Negative Index

\n\n

The most popular and concise method. The substr() function accepts a negative length value to count characters from the end of the string.

\n\n
<?php\n$string = "Hello, World!";\n$lastChar = substr($string, -1);\necho $lastChar; // Outputs: !\n?>
\n\n

Pros:

\n
    \n
  • Short and clear
  • \n
  • Fast performance
  • \n
\n\n

Cons:

\n
    \n
  • Does not support multibyte encodings (Cyrillic, CJK characters)
  • \n
  • Can return a "broken" character for UTF-8 strings
  • \n
\n\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\n

Why 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.

\n\n

Method #2: The Correct One — mb_substr() for Multibyte Strings

\n\n

If your project works with Unicode (which is practically mandatory today), use mb_substr() from the mbstring extension.

\n\n
<?php\n$string = "Привет, мир!";\n$lastChar = mb_substr($string, -1);\necho $lastChar; // Outputs: !\n?>
\n\n

Now for the fun part — working with emojis:

\n\n
<?php\n$string = "👋 Привет 🌍";\n$lastChar = mb_substr($string, -1);\necho $lastChar; // Outputs: 🌍\n?>
\n\n

Pros:

\n
    \n
  • Correctly handles any encoding (UTF-8, UTF-16, etc.)
  • \n
  • Works with emojis and special characters
  • \n
\n\n

Cons:

\n
    \n
  • Requires the mbstring extension to be installed (available on almost all hosting providers)
  • \n
  • Slightly slower than substr()
  • \n
\n\n

Method #3: An Alternative — str_split() and end()

\n\n

You 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\n

Note: str_split() also doesn't work well with multibyte strings. For Cyrillic, you need to use preg_split() with the Unicode flag:

\n\n
<?php\n$string = "Код";\n$chars = preg_split('//u', $string, -1, PREG_SPLIT_NO_EMPTY);\n$lastChar = end($chars);\necho $lastChar; // Outputs: д\n?>
\n\n

Pros:

\n
    \n
  • Intuitive code
  • \n
  • You can easily get not only the last character but any character
  • \n
\n\n

Cons:

\n
    \n
  • Excessive memory usage (creates an array)
  • \n
  • Slower than direct functions
  • \n
\n\n

Method #4: Advanced — iconv_substr() for Specific Encodings

Blogs

Book Recommendations