rtrim Function in PHP 8.2, PHP 8.3 & PHP 8.4


The `rtrim()` function in PHP removes whitespace or other specified characters from the end of a string. It's been available in PHP for many versions and works the same way in PHP 8. PHP 8.1, PHP 8.2, PHP 8.3,and PHP 8.4.

Basic Syntax

<?php
rtrim(string $string, string $characters = " \n\r\t\v\x00"): string
?>

Example-1. Basic Usage (removing whitespace)

<?php
$text = "Hello World ";
echo "'" . rtrim($text) . "'";
// Output: 'Hello World'
?>

Notes
1-`rtrim()` is binary-safe
2- For removing from the start of the string, use `ltrim()`
3- To remove from both ends, use `trim()`
4- The function doesn't modify the original string but returns a new one

PHP 8.2 doesn't introduce any changes to this function - it works the same way as in previous PHP versions.