Date:

Share:

Get Last Character of String PHP

Related Articles


Getting the last character of a string with PHP is pretty simple, all you need is one line of code and your string to be deployed.

Let’s take a demo string such as ‘This is my string!’

We need to catch the last character from the string which of course will be the exclamation point.

For this task, substr() Is the perfect function.

PHP

$string = "This is my string!";

$lastChar = substr($string, -1);

echo $lastChar; // outputs !

So h substr () The function gets a string and some numeric parameters to ensure that the correct parts of the string are deleted.

In this case we use the string parameter which is our string and the second parameter as -1 (offset), says substr() To start at the end of the string and take the last character.

See documentation on substr () Function for more complex examples.





Copyright 2021 Code Wall. All rights reserved.


Source

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Popular Articles