How can PHP beginners effectively handle scenarios where user preferences need to be pre-selected in dropdown menus?

When dealing with pre-selected user preferences in dropdown menus, PHP beginners can use the selected attribute in HTML to set the desired option as selected. This can be achieved by checking if the user's preference matches the value of each option in the dropdown menu and adding the selected attribute accordingly.

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