Last Character of a String in PHP: 5 Simple Ways and Common Mistakes

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 Beyond



Have you ever encountered a situation where you need to quickly extract the last character from a string? Maybe you're checking a file extension, processing user input, or simply want to put an exclamation mark at the end of a phrase. It seems like a simple task — but how many pitfalls does it hide!



On Stack Overflow, this question has garnered hundreds of thousands of views, with dozens of comments offering alternative solutions. It turns out that even such a simple operation as getting the last character of a string in PHP has its nuances: from working with multibyte encodings to non-obvious bugs.



In this article, we'll cover 5 ways to get the last character of a string, debunk popular myths, and show you how to avoid common mistakes. Let's go!



1. The Classic Way: substr() and strlen()



The most common method you'll find in any textbook is using a combination of substr() and strlen(). Let's recall how it works:



<?php$string = "Hello, world!";$lastChar = substr($string, -1); // Using a negative indexecho $lastChar; // Outputs "!"?>


Starting with PHP 7.1, we can use negative values in substr(). This is the most concise and understandable way. Previously, you had to write it like this:



<?php$string = "Hello World";$lastChar = substr($string, strlen($string) - 1); // A more cumbersome optionecho $lastChar; // "d"?>


Important! This method works great with ASCII characters (Latin letters, numbers, punctuation marks). But if your string contains Cyrillic, emojis, or other multibyte characters — read on.



2. The UTF-8 Problem: Why substr() Doesn't Work with Cyrillic



Imagine: you're writing a website in Russian and trying to get the last character of the string "Привет" using substr(). What will you see?



<?php$string = "Привет";$lastChar = substr($string, -1);echo $lastChar; // Outputs "�" or nothing at all?>


Why does this happen? The thing is, Cyrillic characters take up 2 bytes in UTF-8 encoding (and some emojis take up to 4 bytes). The substr() function works at the byte level, not the character level. It "cuts off" one byte, splitting the character in half — hence the garbled text.



The solution is to use multibyte functions. PHP has a whole family of mb_* functions for this.



3. The Correct Way: mb_substr() for Multibyte Strings



If your application works with Unicode (which is 99% of cases today), use mb_substr():



<?php$string = "Привет, мир!";$lastChar = mb_substr($string, -1, 1, 'UTF-8');echo $lastChar; // Correctly outputs "!"?>


Pay attention to the parameters:

  • -1 — start from the end of the string
  • 1 — take one character
  • 'UTF-8' — specify the encoding (mandatory!)


Without specifying the encoding, mb_substr() will use the internal encoding, which may differ from your string's encoding. Always explicitly set the encoding!



4. Alternative Methods: str_split, array_pop, and Loops



Sometimes it's useful to know non-standard approaches. Here are a few more ways to get the last character:



4.1. Via str_split() and array_pop()



<?php$string = "PHP";$chars = str_split($string);$lastChar = array_pop($chars);echo $lastChar; // "P"?>


Downside: creates an array, which is inefficient for long strings. Upside:

Blogs

Book Recommendations