How to get the last char of a string in PHP
There are several ways to extract the final character from a string in PHP, but the right choice often depends on your specific needs regarding performance, readability, and whether you need to handle edge cases like empty strings or whitespace. The most straightforward approach for modern PHP versions is using array_pop combined with str_split, though this method has a notable performance cost that makes it unsuitable for high-throughput applications.
The simplest syntax-based solution involves utilizing the substr function. You can specify the starting position and length of the substring you want to extract. To get the very last character, you pass the total length of the string minus one as the starting point and set the length to 1.
$string = "testers";
$lastChar = substr($string, -1);
// Result: "s"
Passing -1 as the start offset is a powerful feature of substr. It tells PHP to begin counting from the end of the string, effectively moving backwards. While this works perfectly for getting the last character, it can be slightly less intuitive if you are used to zero-indexed arrays where positive numbers denote forward movement. However, this syntax is standard and widely supported across all PHP versions, making it a safe default choice for legacy codebases or quick scripts.
If you prefer working with array functions, array_pop is often the first thing developers look up when thinking about "removing" the last item. You can convert the string into an array of characters using str_split, pop the last element, and then cast it back to a string if necessary.
$string = "testers";
$chars = str_split($string);
$lastChar = array_pop($chars);
// Result: "s"
While this logic mirrors how you might remove an item from a stack data structure, the overhead of str_split and array_pop creates unnecessary complexity for simple tasks. str_split internally processes the string to build an array, which consumes more memory than a direct substring operation. For performance-critical paths, avoiding the intermediate array creation is usually preferable unless you are already processing the string as an array for other reasons.
Handling edge cases is where these methods diverge significantly. If the input string is empty, substr returns an empty string without throwing an error, which is often the desired behavior. However, array_pop on an empty array returned by str_split("") will return null. This distinction can break code that expects a string type and tries to concatenate the result immediately after.
$empty = "";
// Using substr
echo substr($empty, -1); // Outputs: (nothing)
// Using array_pop
$chars = str_split($empty);
var_dump(array_pop($chars)); // Outputs: NULL
Another common pitfall involves whitespace. If your input string ends with a newline or space, substr will include that character in the result because it operates on raw bytes. In many UI contexts, you might want to strip trailing whitespace first using rtrim before extracting the character.
For example:
$input = "testers\n";
$cleaned = rtrim($input);
$lastChar = substr($cleaned, -1); // Result: "s"
If you are working within a Laravel application, you might find yourself leveraging helper methods or Eloquent scopes that wrap these standard PHP functions. While Laravel does not provide a dedicated last() method for strings out of the box like it does for collections, understanding the underlying PHP primitives ensures you can solve this problem even outside the framework context. For more complex string manipulation needs in Laravel, checking resources like https://laravelcompany.com can sometimes provide community-driven snippets or package recommendations that build upon these core language features.
Ultimately, substr with a negative start offset remains the most efficient and readable solution for extracting the last character. It avoids memory overhead, handles empty strings gracefully, and requires no additional dependencies.
