What role does the nl2br function play in preserving formatting when retrieving text from a database in PHP?
When retrieving text from a database in PHP, the nl2br function is used to preserve line breaks and formatting that may have been stored in the database. This function converts new line characters (\n) into HTML line breaks (<br>) so that the text displays correctly in a web browser.
// Retrieve text from database
$text = "This is a text with\nline breaks.";
// Preserve formatting using nl2br
$formattedText = nl2br($text);
// Display the formatted text
echo $formattedText;
Related Questions
- In what ways can a PHP developer ensure proper implementation of Object-Oriented Programming principles when working on projects like a Webshop, as advised in the forum discussion?
- What are some best practices for comparing and working with passed numbers in PHP arrays?
- Are there any best practices or recommendations for handling authentication in Nusoap without using setCredentials?