What are the best practices for handling initial display and display after form submission in PHP when setting the "selected" value in an HTML SELECT form?

When handling initial display and display after form submission in PHP with HTML SELECT forms, it is important to set the "selected" attribute for the option that matches the submitted or default value. This can be achieved by checking if the submitted value matches each option value and adding the "selected" attribute accordingly.

<select name="example">
    <option value="1" <?php echo ($_POST['example'] == '1') ? 'selected' : ''; ?>>Option 1</option>
    <option value="2" <?php echo ($_POST['example'] == '2') ? 'selected' : ''; ?>>Option 2</option>
    <option value="3" <?php echo ($_POST['example'] == '3') ? 'selected' : ''; ?>>Option 3</option>
</select>