How does the trim() function in PHP work and what does it remove from a string?

The trim() function in PHP removes any leading or trailing whitespace (spaces, tabs, newlines) from a string. This can be useful when working with user input or data from external sources to ensure consistency in the data format.

$string = "   Hello, World!   ";
$trimmedString = trim($string);
echo $trimmedString; // Output: "Hello, World!"