How can a foreach loop be used to iterate through an array of database values and generate options for an HTML select element in PHP?

To iterate through an array of database values and generate options for an HTML select element in PHP, you can use a foreach loop to loop through the array and create an option element for each value. Within the loop, you can output the value as the option's text and set the value attribute to the value itself. This way, each database value will be represented as an option in the select element.

<?php
// Assume $dbValues is an array of database values
$dbValues = ['Value 1', 'Value 2', 'Value 3'];

echo '<select>';
foreach ($dbValues as $value) {
    echo '<option value="' . $value . '">' . $value . '</option>';
}
echo '</select>';
?>