What are some best practices for echoing the value of a select field in PHP?

When echoing the value of a select field in PHP, it is important to ensure that the selected option is displayed correctly. One way to achieve this is by using an if statement to check if the value of each option matches the selected value, and then adding the 'selected' attribute to the corresponding option tag.

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