How can PHP be used to maintain the original formatting of a text when outputting it to a webpage?

When outputting text to a webpage using PHP, the original formatting such as line breaks and spaces can be lost. To maintain the original formatting, you can use the nl2br() function in PHP. This function converts newlines (\n) in a string to HTML line breaks (<br>), preserving the original formatting when displayed on a webpage.

&lt;?php
$text = &quot;This is some text with\nline breaks.&quot;;
echo nl2br($text);
?&gt;