What is the purpose of using the "selected" attribute in a select box in PHP forms?

The "selected" attribute in a select box in PHP forms is used to pre-select an option in the dropdown list when the form is loaded. This is useful when you want to display a default option or have a specific option pre-selected based on certain conditions.

<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>