What are some best practices for handling line breaks and formatting in text output on a webpage using PHP functions like nl2br()?

When displaying text on a webpage that includes line breaks, it is important to properly handle them for consistent formatting. One common approach is to use the PHP nl2br() function, which converts newline characters (\n) to HTML <br> tags. This allows the text to display with proper line breaks in HTML format.

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