How can special characters, like single quotes, be properly displayed in HTML form fields when retrieved from a database in PHP?
Special characters, like single quotes, can be properly displayed in HTML form fields when retrieved from a database in PHP by using the htmlspecialchars() function to convert the characters into their corresponding HTML entities. This function will ensure that the special characters are displayed correctly in the form fields without causing any issues with the HTML markup.
// Retrieve data from the database
$data = "This is a test with 'single quotes'";
// Display the data in an HTML form field
echo '<input type="text" value="' . htmlspecialchars($data) . '">';