What is the function nl2br() used for in PHP and how does it relate to line breaks in database storage?

The function nl2br() in PHP is used to convert newline characters (\n) into HTML line breaks (<br>) in a string. This is particularly useful when displaying text from a database that contains line breaks, as HTML does not recognize newline characters by default. By using nl2br(), the line breaks in the database-stored text will be properly displayed on the webpage.

&lt;?php
// Retrieve text from database with line breaks
$text = &quot;This is a text with\nline breaks.&quot;;

// Convert newline characters to HTML line breaks
$text_with_line_breaks = nl2br($text);

// Display the text with line breaks on the webpage
echo $text_with_line_breaks;
?&gt;