PHP Functions : Howto Remove Last Character From String In PHP
To remove last character from a string in php; Through this tutorial, You will learn how to remove last character string form with example. When you work with PHP, Many time, you want to you remove characters from strings. This tutorial shows you various PHP functions for removing the last character from the given string.

This tutorial demonstrates an easy way for you. To remove the last character from string in PHP.
1. Method First – substr function
You can use the substr function of php for remove the last character from string in php.
Syntax:
The syntax of subster method is given below:
substr($string, 0, -1);
Example – Method First – substr function
$string = "Hello World!"; echo "Given string: " . $string . "\n"; echo "Updated string: " . substr($string, 0, -1) . "\n";
Output
Given string: Hello World! Updated string: Hello World
2. Method Second – substr_replace function
You can also use substr_replace for the remove the last character from string in php.
Syntax:
The basic syntax of substr_replace function is:
substr_replace($string ,"", -1);
Example – substr_replace function
$string = "Hello World!"; echo "Given string: " . $string . "\n"; echo "Updated string: " . substr_replace($string ,"",-1) . "\n";
Output
Given string: Hello World! Updated string: Hello World
3. Method Third – rtrim() function
You can use the PHP rtrim() function to remove the last character from the given string in PHP.
Syntax:
The basic syntax of rtrim() function is:
rtrim($string,'a');
Here “a” is the character that you want to remove in your string.
Example – rtrim() function
$string = "Hello World!"; echo "Given string: " . $string . "\n"; echo "Updated string: " . rtrim($string, "!") . "\n";
Output
Given string: Hello World! Updated string: Hello World
Question:- php remove last character from string if comma?
Answer:- If you have a comma separeted string in php and you want to remove last character from string if comma or remove last comma from string PHP, so you can use PHP rtrim() function like below:
$string = "remove comma from end of string php,"; echo "Given string: " . $string . "\n"; echo "Updated string: " . rtrim($string, ",") . "\n";
Output
Given string: remove comma from end of string php, Updated string: remove comma from end of string php
Conclusion
PHP Remove the last character from a string. In this tutorial, you have many methods of PHP to remove the last character from any given strings.