Can the selected option in a select dropdown be maintained using PHP?

When using PHP to generate a select dropdown, you can maintain the selected option by checking each option against a variable that holds the selected value. If the option matches the selected value, you can add the "selected" attribute to that option. This way, when the form is submitted and reloaded, the selected option will remain selected.

<select name="dropdown">
    <option value="option1" <?php echo ($selected == 'option1') ? 'selected' : ''; ?>>Option 1</option>
    <option value="option2" <?php echo ($selected == 'option2') ? 'selected' : ''; ?>>Option 2</option>
    <option value="option3" <?php echo ($selected == 'option3') ? 'selected' : ''; ?>>Option 3</option>
</select>