How does the nl2br() function in PHP help in handling line breaks in text output?

When outputting text in HTML, line breaks in the original text may not be displayed correctly. The nl2br() function in PHP helps by converting newline characters (\n) to HTML line break tags (<br>) so that line breaks are properly displayed in the browser.

$text = &quot;This is a text with
line breaks.&quot;;
echo nl2br($text);