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

Matheus Mello
Matheus Mello
September 2, 2023
Cover Image for How to get the last char of a string in PHP?

😎 Easy-Peasy Ways to Get the Last Character of a String in PHP! 💪

So, you've come across a situation where you need to extract the last character of a string in PHP. Fear not, my tech-savvy friend, because I've got your back! 👊

Here's the scenario – you have a string, let's use "testers" as an example, and you want to grab the very last character, which in this case is "s". Ready to dive into some code? Let's go! 🏊‍♂️

Solution 1️⃣: Utilizing substr()

One of the simplest and easiest ways to get the last character of a string in PHP is by using the substr() function. This nifty function allows you to extract a portion of a string by specifying the starting position and the length. In our case, we want the last character, so we need to specify a negative length of 1:

$string = "testers";
$lastChar = substr($string, -1);
echo $lastChar; // Output: s

Voila! 🎉 The $lastChar variable now holds the desired last character of the string. You can manipulate this variable further or carry out any other operations with it.

Solution 2️⃣: Leveraging mb_substr()

If you're working with multibyte characters (such as emojis or certain special characters), the mb_substr() function is your go-to option. It works similarly to substr() but handles multibyte characters correctly.

$string = "testers";
$lastChar = mb_substr($string, -1);
echo $lastChar; // Output: s

And just like that, you've conquered the multibyte character obstacle! 🌟

Solution 3️⃣: Exploring String-to-Array Conversion

Another approach, especially when you want to explore each character of the string in more detail, is to convert the string into an array using str_split() and then grabbing the last element:

$string = "testers";
$characters = str_split($string);
$lastChar = end($characters);
echo $lastChar; // Output: s

This method gives you more flexibility to perform operations on individual characters if needed. 🎭

Your Turn! 🙌

Now that you have a selection of solutions to choose from, it's time for you to give them a try! Test them out in your code and see which one suits your specific needs the best. Don't be afraid to experiment and have fun with it! 💡

Have Other PHP Questions? Join Our Tech Community! 💬

If you found this guide helpful, we have plenty more tech goodies waiting for you! Join our tech community, where we share tips, tricks, and insights on various programming languages and technologies. 🤓

Feel free to leave a comment below if you have any questions or want to share your own experiences with extracting the last character of a string in PHP. Let's learn and grow together! 🌱

Take Your Tech Career to the Next Level

Our application tracking tool helps you manage your job search effectively. Stay organized, track your progress, and land your dream tech job faster.

Your Product
Product promotion

Share this article

More Articles You Might Like

Latest Articles

Cover Image for How can I echo a newline in a batch file?
batch-filenewlinewindows

How can I echo a newline in a batch file?

Published on March 20, 2060

🔥 💻 🆒 Title: "Getting a Fresh Start: How to Echo a Newline in a Batch File" Introduction: Hey there, tech enthusiasts! Have you ever found yourself in a sticky situation with your batch file output? We've got your back! In this exciting blog post, we

Cover Image for How do I run Redis on Windows?
rediswindows

How do I run Redis on Windows?

Published on March 19, 2060

# Running Redis on Windows: Easy Solutions for Redis Enthusiasts! 🚀 Redis is a powerful and popular in-memory data structure store that offers blazing-fast performance and versatility. However, if you're a Windows user, you might have stumbled upon the c

Cover Image for Best way to strip punctuation from a string
punctuationpythonstring

Best way to strip punctuation from a string

Published on November 1, 2057

# The Art of Stripping Punctuation: Simplifying Your Strings 💥✂️ Are you tired of dealing with pesky punctuation marks that cause chaos in your strings? Have no fear, for we have a solution that will strip those buggers away and leave your texts clean an

Cover Image for Purge or recreate a Ruby on Rails database
rakeruby-on-railsruby-on-rails-3

Purge or recreate a Ruby on Rails database

Published on November 27, 2032

# Purge or Recreate a Ruby on Rails Database: A Simple Guide 🚀 So, you have a Ruby on Rails database that's full of data, and you're now considering deleting everything and starting from scratch. Should you purge the database or recreate it? 🤔 Well, my