How can the nl2br function in PHP be used to handle line breaks in text stored in a database?

When text is stored in a database, line breaks are often represented by newline characters (\n) which may not be displayed correctly in HTML. The nl2br function in PHP can be used to convert these newline characters to HTML line breaks (<br>) so that the text displays properly on a webpage. To use nl2br with text retrieved from a database, simply apply the function to the text before displaying it on the webpage.

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

// Apply nl2br function to convert newline characters to HTML line breaks
$formatted_text = nl2br($text);

// Display the formatted text on the webpage
echo $formatted_text;