How can the use of nl2br function affect the output of PHP scripts?

The nl2br function in PHP converts newline characters (\n) to HTML line breaks (<br>). This can affect the output of PHP scripts by altering the formatting of text displayed on a webpage. To solve this issue, you can use the nl2br function only when displaying text that contains newline characters to ensure proper line breaks are inserted in the HTML output.

$text = &quot;This is a text with\na newline character.&quot;;
echo nl2br($text);