What is the best practice for storing line breaks in a database and displaying them later in PHP?
When storing line breaks in a database, it's best to use a consistent format such as storing them as newline characters (\n) or using a specific delimiter to represent line breaks. When displaying the stored text in PHP, you can use the nl2br() function to convert newline characters to HTML line breaks for proper formatting on the web page.
// Storing line breaks in the database
$text = "This is a\nmultiline\ntext";
$escaped_text = mysqli_real_escape_string($connection, $text);
$query = "INSERT INTO table_name (text_column) VALUES ('$escaped_text')";
mysqli_query($connection, $query);
// Displaying stored text with line breaks in PHP
$query = "SELECT text_column FROM table_name WHERE id = 1";
$result = mysqli_query($connection, $query);
$row = mysqli_fetch_assoc($result);
echo nl2br($row['text_column']);