How can special characters, such as quotes, be properly transmitted in PHP when retrieved from a database for use in a dropdown menu?

Special characters, such as quotes, can be properly transmitted in PHP by using the htmlspecialchars() function when retrieving the data from the database. This function converts special characters to HTML entities, ensuring they are displayed correctly in the dropdown menu.

// Retrieve data from the database
$data = fetchDataFromDatabase();

// Loop through the data and apply htmlspecialchars() function
foreach ($data as $item) {
    $item['name'] = htmlspecialchars($item['name'], ENT_QUOTES);
    echo "<option value='{$item['id']}'>{$item['name']}</option>";
}