What are best practices for handling form submissions with radio buttons in PHP?

When handling form submissions with radio buttons in PHP, it is important to validate the input to ensure that only one radio button is selected. This can be done by checking if the radio button value is set and matches the expected value. Additionally, sanitize the input to prevent any malicious code injection.

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    if(isset($_POST['radio_button']) && ($_POST['radio_button'] == 'option1' || $_POST['radio_button'] == 'option2' || $_POST['radio_button'] == 'option3')) {
        // Process the form submission
        $selected_option = $_POST['radio_button'];
        // Sanitize the input if needed
        $sanitized_option = filter_var($selected_option, FILTER_SANITIZE_STRING);
    } else {
        // Handle invalid input
        echo "Invalid selection";
    }
}
?>