PHP Get Last Character from String Example

03-Apr-2023

.

Admin

PHP Get Last Character from String Example

Hi Dev,

In this tutorial, you will learn php get last Character from string example. we will help you to give example of how to get last Character from string in php?. We will use get last Character from string in php example. it's simple example of how to use function to get last Character from string in php?.

There are three example to get Last Character from strings in PHP. in this example, we will use to substr(), mb_substr() and strlen() function to get Last Character. so Let's both the following example with output.

Example 1: Get Last Character from String


index.php

<?php

$str = "Nicesnippets";

//Get Last character from a string

$result = substr($str, strlen($str)-1);

echo $result;

?>

Output:

s

Example 2: Get Last Character from String

index.php

<?php

$str = "Hello World";

//Get Last character from a string

$result = mb_substr($str, -1);

echo $result;

?>

Output:

d

Example 3: Get Last Character from String

index.php

<?php

$str = "Welcome";

//Get Last character from a string

$lastChar = strlen($str) -1;

$result = $str[$lastChar];

echo $result;

?>

Output:

e

I hope it could help you...

#PHP