What are common pitfalls when using selected attributes in HTML options for PHP forms?

One common pitfall when using selected attributes in HTML options for PHP forms is forgetting to set the selected attribute for the option that should be pre-selected. To solve this, make sure to check the value of the selected option and add the selected attribute if it matches the desired value.

<select name="color">
    <option value="red" <?php echo ($color == 'red') ? 'selected' : ''; ?>>Red</option>
    <option value="blue" <?php echo ($color == 'blue') ? 'selected' : ''; ?>>Blue</option>
    <option value="green" <?php echo ($color == 'green') ? 'selected' : ''; ?>>Green</option>
</select>