What is the best practice for evaluating user input from dynamic radio buttons in PHP?

When evaluating user input from dynamic radio buttons in PHP, the best practice is to use an associative array to store the radio button values and their corresponding labels. This allows for easy retrieval and validation of the user-selected value.

// Example of evaluating user input from dynamic radio buttons in PHP

// Define an associative array to store radio button values and labels
$radio_buttons = array(
    'option1' => 'Option 1',
    'option2' => 'Option 2',
    'option3' => 'Option 3'
);

// Check if the form has been submitted
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // Check if the selected radio button value is valid
    if (isset($_POST['radio_button']) && array_key_exists($_POST['radio_button'], $radio_buttons)) {
        $selected_option = $_POST['radio_button'];
        echo "You selected: " . $radio_buttons[$selected_option];
    } else {
        echo "Invalid selection";
    }
}