What is the purpose of using trim() function in PHP?

The trim() function in PHP is used to remove whitespace or other specified characters from the beginning and end of a string. This can be useful when dealing with user input or data from external sources where extra spaces may be present. By using trim(), you can ensure that the string is clean and ready for further processing.

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