How can nl2br() be effectively used for line breaks in HTML text within PHP scripts?

To insert line breaks in HTML text within PHP scripts, the nl2br() function can be used. This function converts newlines (\n) in a string to HTML line breaks (<br>). Simply apply nl2br() to the string where line breaks are needed before outputting it to the HTML page.

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