What is the purpose of using the trim() function in PHP and how can it help with removing line breaks in strings?

When working with strings in PHP, it is common to encounter unwanted whitespace characters such as line breaks at the beginning or end of the string. This can be problematic when comparing strings or displaying them on a webpage. The trim() function in PHP is used to remove whitespace characters from the beginning and end of a string, including line breaks, making it a useful tool for cleaning up strings.

$string = "   Hello, world!   \n";
$trimmedString = trim($string);
echo $trimmedString;