What is the function nl2br() used for in PHP and how can it help with formatting text input?

The nl2br() function in PHP is used to insert HTML line breaks (<br>) before all newlines in a string. This can help with formatting text input by converting newline characters into HTML line breaks, making the text display properly in an HTML document.

&lt;?php
$text = &quot;This is a text\nwith newlines&quot;;
$formatted_text = nl2br($text);
echo $formatted_text;
?&gt;