How can the nl2br function be applied to comments before outputting them to ensure proper line breaks?

When displaying comments from a database in a web page, line breaks entered by users in the comment text may not be displayed correctly. To ensure proper line breaks are maintained, the PHP `nl2br()` function can be applied to the comment text before outputting it to the page. This function converts newline characters (`\n`) into HTML line breaks (`<br>`), allowing the comments to be displayed with the intended line breaks.

// Retrieve comment text from database
$comment = &quot;This is a comment with \n line breaks.&quot;;

// Apply nl2br function to the comment text
$comment_with_linebreaks = nl2br($comment);

// Output the comment with proper line breaks
echo $comment_with_linebreaks;