How can PHP arrays and loops be used to optimize code for generating select options in a form?

When generating select options in a form, using PHP arrays and loops can optimize code by reducing repetition and making it easier to manage and update the options. By storing the options in an array and looping through them to generate the select options dynamically, you can avoid hardcoding each option individually.

// Define an array of options
$options = array("Option 1", "Option 2", "Option 3", "Option 4");

// Generate select options using a loop
echo '<select name="select">';
foreach($options as $option) {
    echo '<option value="' . $option . '">' . $option . '</option>';
}
echo '</select>';