What are the potential pitfalls of using the "selected" attribute in PHP code for radio buttons?

The potential pitfall of using the "selected" attribute in PHP code for radio buttons is that it may not properly reflect the user's selection if the form is submitted and the page is reloaded. To solve this issue, you can use PHP to dynamically set the "checked" attribute based on the user's selection.

<form action="submit.php" method="post">
    <input type="radio" name="option" value="option1" <?php if(isset($_POST['option']) && $_POST['option'] == 'option1') echo 'checked'; ?>> Option 1
    <input type="radio" name="option" value="option2" <?php if(isset($_POST['option']) && $_POST['option'] == 'option2') echo 'checked'; ?>> Option 2
    <input type="radio" name="option" value="option3" <?php if(isset($_POST['option']) && $_POST['option'] == 'option3') echo 'checked'; ?>> Option 3
    <input type="submit" value="Submit">
</form>