What are the best practices for handling line lengths and trimming in PHP file operations?

When working with file operations in PHP, it's important to handle line lengths and trimming to ensure clean and readable code. One common practice is to trim whitespace from the beginning and end of each line when reading from a file, and to ensure that lines do not exceed a certain maximum length when writing to a file. This helps maintain consistency and readability in your code.

// Reading from a file and trimming each line
$filename = 'example.txt';
$lines = file($filename, FILE_IGNORE_NEW_LINES);
$trimmedLines = array_map('trim', $lines);

// Writing to a file with maximum line length
$maxLineLength = 80;
$outputFilename = 'output.txt';
$outputLines = array_map(function($line) use ($maxLineLength) {
    return wordwrap($line, $maxLineLength, "\n", true);
}, $trimmedLines);
file_put_contents($outputFilename, implode("\n", $outputLines));