In what situations is it necessary to use functions like nl2br in PHP to ensure that text formatting is preserved and displayed correctly in web applications?

When displaying text in web applications that include line breaks or new lines, it is necessary to use functions like nl2br in PHP to ensure that the formatting is preserved and displayed correctly in HTML. This is because HTML does not recognize plain line breaks or new lines in text, so using nl2br converts them into HTML line breaks <br> that are rendered correctly in the browser.

&lt;?php
$text = &quot;This is a text with
line breaks.&quot;;

echo nl2br($text);
?&gt;