How can PHP developers ensure that default values in select fields are processed correctly in non-self-submitting forms?

When working with non-self-submitting forms in PHP, developers can ensure that default values in select fields are processed correctly by checking if the form has been submitted and setting the selected attribute in the option tag based on the submitted value or the default value.

<form method="post">
    <select name="color">
        <option value="red" <?php if(isset($_POST['color']) && $_POST['color'] == 'red') echo 'selected'; ?>>Red</option>
        <option value="blue" <?php if(isset($_POST['color']) && $_POST['color'] == 'blue') echo 'selected'; ?>>Blue</option>
        <option value="green" <?php if(isset($_POST['color']) && $_POST['color'] == 'green') echo 'selected'; ?>>Green</option>
    </select>
    <input type="submit" value="Submit">
</form>