What is the best way to ensure that the selected entry in a dropdown menu remains after submitting a form in PHP?

When submitting a form in PHP with a dropdown menu, the selected entry may not remain after the form is submitted due to the page reloading. To ensure that the selected entry stays selected after form submission, you can use PHP to check if the submitted value matches each option in the dropdown menu and add the "selected" attribute to the matching option.

<select name="dropdown">
    <option value="option1" <?php if(isset($_POST['dropdown']) && $_POST['dropdown'] == 'option1') { echo 'selected'; } ?>>Option 1</option>
    <option value="option2" <?php if(isset($_POST['dropdown']) && $_POST['dropdown'] == 'option2') { echo 'selected'; } ?>>Option 2</option>
    <option value="option3" <?php if(isset($_POST['dropdown']) && $_POST['dropdown'] == 'option3') { echo 'selected'; } ?>>Option 3</option>
</select>