How to Get the Last Character of a String in PHP

Minahil Noor Feb 02, 2024
  1. Use substr() Function to Get the Last Char of a String in PHP
  2. Use Direct String Access to Get the Last Char of a String in PHP
  3. Use a for Loop to Get the Last Char of a String in PHP
How to Get the Last Character of a String in PHP

In this article, we will introduce methods to get the last character of a string.

  • Using substr() function
  • By direct string access
  • Using for loop

Use substr() Function to Get the Last Char of a String in PHP

The built-in substr() function in PHP could get the substring of a string. This function has three parameters out of which one is optional. The correct syntax to use this function is as follows

substr($stringName, $startingPosition, $lengthOfSubstring);

The $stringName is the input string. The $startngPosition is the starting index of the substring. The $lengthOfSubstring is the length of the substring. The variable $stringName and $startingPosition are mandatory parameters while $lengthOfSubstring is an optional parameter. When $lengthOfSubstring is omitted, the substring will start from $startngPosition until the end of the input string.

<?php  
$string = 'This is a string';
$lastChar = substr($string, -1);
echo "The last char of the string is $lastChar.";
?>

If $startingPosition has a positive value then the substring starts from the left side of the string i.e the start of the string. If $startingPosition has negative then the substring starts from the right side of the string i.e the end of the string.

To get the last char of a string, here we pass -1 as the value of $startingPosition. -1 means the last character of the string.

Output:

The last char of the string is g.

Use Direct String Access to Get the Last Char of a String in PHP

A string is an array of chars. We can get the last char of a string by considering it an array of chars.

<?php  
$string = "This is a string";
$lengthOfString = strlen($string);
$lastCharPosition = $lengthOfString-1;
$lastChar = $string[$lastCharPosition];
echo "The last char of the string is $lastChar.";
?>

The index of the last character of a string is one less than PHP string length because the index starts from 0. The $lengthOfString holds the length of string using the strlen() function. The $lastCharPosition holds the index of last char of the string. The output is the last char after passing $lastCharPosition as an index to the array of chars.

Output:

The last char of the string is g.

Negative String Index Access From PHP 7.1.0

From PHP 7.1.0, the negative index of the string is also supported. Therefore, we could use -1 as the index to get the last character of the string in PHP.

Example Codes:

<?php  
$string = "This is a string";

$lastChar = $string[-1];
echo "The last char of the string is $lastChar.";
?>

Output:

The last char of the string is g.

Use a for Loop to Get the Last Char of a String in PHP

We can get the last char of a string by storing it into a new string and then fetch the last char with a for loop. We will traverse the string and reach the last char.

<?php 
$string = "This is a string";
$lastCharPosition = strlen($string) - 1;  
for ($x = $lastCharPosition; $x < strlen($string); $x++) {
    $newString = $string[$x]; 
} 
echo "The last char of the string is $newString.";
?> 

We will get the index of the last char by using strlen() function and assign it to $x in the for loop.

Output:

The last char of the string is g.

Related Article - PHP String