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

When retrieving text from a database in PHP, developers can ensure that line breaks are properly displayed by using the nl2br() function. This function converts newlines in a string to HTML line breaks (<br>). By applying nl2br() to the retrieved text before displaying it on a webpage, line breaks will be preserved and displayed correctly.

$text = &quot;This is a text with\nline breaks.&quot;;
$display_text = nl2br($text);
echo $display_text;