How can the use of "for", "foreach", and "selected" keywords simplify the process of preselecting options in a dropdown menu in PHP?

To simplify the process of preselecting options in a dropdown menu in PHP, you can use a combination of the "for", "foreach", and "selected" keywords. By iterating through the options using a loop, you can easily determine which option should be preselected based on a condition. The "selected" keyword can then be added to the preselected option to ensure it is displayed as the default choice in the dropdown menu.

<select name="dropdown">
<?php
$options = array("Option 1", "Option 2", "Option 3");
$selectedOption = "Option 2"; // Set the option to be preselected

foreach ($options as $option) {
    if ($option == $selectedOption) {
        echo "<option value='$option' selected>$option</option>";
    } else {
        echo "<option value='$option'>$option</option>";
    }
}
?>
</select>