How can PHP code be used to maintain formatting when retrieving data from a MySQL database for display on a webpage?
When retrieving data from a MySQL database to display on a webpage using PHP, you can maintain formatting by using the PHP `nl2br()` function. This function converts new line characters (\n) to HTML line breaks (<br>) so that the text appears correctly formatted on the webpage.
<?php
// Retrieve data from MySQL database
$data = "This is a text with \n new line characters.";
// Display data on webpage with formatting maintained
echo nl2br($data);
?>
Keywords
Related Questions
- What are some potential pitfalls to consider when developing a custom admin system in PHP?
- Is it recommended to allow users to select data based on names rather than IDs in PHP applications?
- Welche Best Practices gibt es für die Verwendung von Sessions, Cookies und htaccess in PHP für die Sicherheit von Login-Systemen?