How to Get Last Character from String Example in PHP

BOOKC, this time share about How to Get Last Character from String Example in PHP

In this tutorial, you will learn php to get or retrieve the last character of an example string. we will help you to give an example how to get the last Character of a string in php ? We will use get the last Character of the string in the php example. that's a simple example how to use a function to get the last Character of a string in php ?

There are three examples to get the Last Character of a string in PHP. In this example, we will use the to

  • substr()
  • mb_substr()
  • strlen()

functions to get the Last Character. so Let's take the following example with the output.

Example 1: Get Last Character from String

index.php

<?php  	
   $str = "Bookc";
   //Get Last character from a string
   $result = substr($str, strlen($str)-1);
   echo $result; 
?>

Output:

c

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

So many articles to get the Last Character of a string in PHP. May be useful.

 

Bookc
Admin

Hopefully the information we provide can be useful.

Press ESC to close