What are the best practices for handling line breaks or new lines when echoing data from a MySQL table in PHP?

When echoing data from a MySQL table in PHP, line breaks or new lines may not be displayed correctly due to the way the data is stored in the database. To ensure that line breaks are properly displayed, you can use the PHP nl2br() function to convert newline characters to HTML line breaks before echoing the data.

<?php
// Retrieve data from MySQL table
$data = "This is a\nnew line.";

// Echo the data with line breaks converted to HTML
echo nl2br($data);
?>