Are there any best practices for preselecting radio buttons in PHP forms?

When preselecting radio buttons in PHP forms, you can achieve this by checking if the value of the radio button matches a specific value in the form submission data. If it matches, you can add the "checked" attribute to the radio button input tag to preselect it.

<form action="submit.php" method="post">
  <input type="radio" name="gender" value="male" <?php if(isset($_POST['gender']) && $_POST['gender'] == 'male') echo 'checked'; ?>> Male
  <input type="radio" name="gender" value="female" <?php if(isset($_POST['gender']) && $_POST['gender'] == 'female') echo 'checked'; ?>> Female
  <input type="submit" value="Submit">
</form>