Skip to content
PHP Logo

How can developers ensure that line breaks are displayed correctly when retrieving text from a database in PHP?

When retrieving text from a database in PHP, developers should make sure to use the nl2br() function to convert newline characters into <br> tags. This will ensure that line breaks are displayed correctly when the text is rendered on a webpage.

php
$text_from_database = "This is a text with\nline breaks.";
$text_with_line_breaks = nl2br($text_from_database);

echo $text_with_line_breaks;