How can PHP be used to retain the selected radio button value when refreshing a page?

When a radio button is selected on a form and the page is refreshed, the selected value is lost because the form is reset. To retain the selected radio button value when refreshing the page, you can use PHP to store the selected value in a session variable and then set the radio button's checked attribute based on the stored value.

<?php
session_start();

$selectedValue = isset($_POST['radio_button']) ? $_POST['radio_button'] : '';

if(!empty($selectedValue)){
    $_SESSION['selected_radio'] = $selectedValue;
}

$checkedA = ($selectedValue == 'A') ? 'checked' : '';
$checkedB = ($selectedValue == 'B') ? 'checked' : '';
$checkedC = ($selectedValue == 'C') ? 'checked' : '';
?>

<form method="post">
    <input type="radio" name="radio_button" value="A" <?php echo $checkedA; ?>> Option A<br>
    <input type="radio" name="radio_button" value="B" <?php echo $checkedB; ?>> Option B<br>
    <input type="radio" name="radio_button" value="C" <?php echo $checkedC; ?>> Option C<br>
    <input type="submit" value="Submit">
</form>