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 = "This is a text with\nline breaks";
// Use nl2br() to convert '\n' to '<br>'
$formatted_text = nl2br($text);
// Display the formatted text
echo $formatted_text;