What is the difference between the trim() and rtrim() functions in PHP, and when should each be used?
The trim() function in PHP removes whitespace (or other specified characters) from both the beginning and end of a string. On the other hand, the rtrim() function specifically removes whitespace (or other specified characters) only from the end of a string. Use trim() when you want to remove leading and trailing whitespace from a string. Use rtrim() when you specifically want to remove trailing whitespace from a string.
// Example of using trim()
$string = " Hello, World! ";
$trimmedString = trim($string);
echo $trimmedString; // Output: "Hello, World!"
// Example of using rtrim()
$string = " Hello, World! ";
$rtrimmedString = rtrim($string);
echo $rtrimmedString; // Output: " Hello, World!"