How can nl2br function in PHP be used to handle line breaks in text output?

When outputting text in PHP, line breaks entered by the user may not be displayed correctly in the browser. The nl2br function in PHP can be used to convert newline characters (\n) to HTML line breaks (<br>) so that the text is displayed with proper line breaks.

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