How can the use of nl2br() impact the formatting of text retrieved from a database in PHP?

When text is retrieved from a database in PHP, line breaks are often stored as '\n' characters. If this text is displayed on a webpage without converting these line breaks, the formatting will not be preserved. Using the nl2br() function in PHP will convert these '\n' characters to '<br>' tags, allowing the text to be displayed with proper line breaks on the webpage.

// Retrieve text from database
$text = &quot;This is a text with\nline breaks&quot;;

// Use nl2br() to convert &#039;\n&#039; to &#039;&lt;br&gt;&#039;
$formatted_text = nl2br($text);

// Display the formatted text
echo $formatted_text;