What is the purpose of the rtrim() function in PHP and how does it work?

The rtrim() function in PHP is used to remove whitespace or other specified characters from the end of a string. This can be useful when you want to clean up user input or remove trailing spaces from a string before further processing. The function takes two parameters: the string to be trimmed and an optional list of characters to be removed.

// Example of using rtrim() function
$string = "Hello World   ";
$trimmed_string = rtrim($string);
echo $trimmed_string; // Output: "Hello World"