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

When handling radiobutton values in PHP forms, it is important to ensure that only one option is selected at a time. One way to achieve this is by giving each radiobutton input a unique name attribute and assigning the same value to all options within a group. Then, in the PHP script that processes the form data, you can check which radiobutton was selected by accessing the value of the corresponding name attribute.

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

<?php
if(isset($_POST['submit'])){
    if(isset($_POST['color'])){
        $selected_color = $_POST['color'];
        echo "Selected color: " . $selected_color;
    } else {
        echo "Please select a color.";
    }
}
?>