How can the PHP function "trim" be used to address issues with whitespace in text file data?

When reading data from text files in PHP, there may be issues with leading or trailing whitespace that can affect the output or processing of the data. The PHP function "trim" can be used to remove any whitespace from the beginning and end of a string, ensuring that the data is clean and consistent.

$file = 'data.txt';
$data = file_get_contents($file);
$cleaned_data = trim($data);

echo $cleaned_data;