How can PHP maintain the formatting of a text file when displaying it in HTML?

When displaying a text file in HTML using PHP, the formatting may be lost due to HTML interpreting special characters differently. To maintain the formatting, you can use the `nl2br()` function in PHP, which converts newline characters to `<br>` tags. This will preserve the line breaks in the text file when displayed in HTML.

&lt;?php
$file = &#039;example.txt&#039;;
$text = file_get_contents($file);
$formatted_text = nl2br($text);

echo $formatted_text;
?&gt;