What are some best practices for handling radio button values in PHP forms?

When handling radio button values in PHP forms, it is important to ensure that only one option can be selected at a time. This can be achieved by giving each radio button in a group the same name attribute, but different values. When the form is submitted, you can use PHP to check which radio button was selected and process the corresponding value accordingly.

<form method="post" action="process_form.php">
  <input type="radio" name="color" value="red"> Red
  <input type="radio" name="color" value="blue"> Blue
  <input type="radio" name="color" value="green"> Green
  <input type="submit" value="Submit">
</form>

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
  $selectedColor = $_POST["color"];
  
  switch($selectedColor) {
    case "red":
      echo "You selected red!";
      break;
    case "blue":
      echo "You selected blue!";
      break;
    case "green":
      echo "You selected green!";
      break;
    default:
      echo "Please select a color.";
  }
}
?>