What best practices should be followed when handling text files in PHP to ensure proper output?

When handling text files in PHP, it is essential to properly handle encoding to ensure that the output is displayed correctly. One common issue is outputting text files with special characters or non-UTF-8 encoding, which can result in garbled or incorrect output. To address this, it is recommended to use functions like `mb_convert_encoding()` to convert the file contents to UTF-8 before outputting.

// Read the text file contents
$fileContents = file_get_contents('example.txt');

// Convert the file contents to UTF-8 encoding
$fileContents = mb_convert_encoding($fileContents, 'UTF-8', 'auto');

// Output the converted file contents
echo $fileContents;