PHP MySql:Checked Dynamic radio Box [关闭]

I have this code for dynamic checked radio box:

PHP:

if ($author === 0) {$checked = 'checked';} else {$checked == '';}

if ($author === 1) {$checked = 'checked';} else {$checked == '';}

if ($author === 2) {$checked = 'checked';} else {$checked == '';}

HTML:

<input type="radio" name="test" <?PHP echo $checked; ?> />
<input type="radio" name="test" <?PHP echo $checked; ?> />
<input type="radio" name="test" <?PHP echo $checked; ?> />

This way is true? What’s is a better/optimized Way?

Can I write any PHP function or class for save code and checked any radio box?

It can save you some lines and unnecessary extra variables if you look forward using the shorten if statement form. Example with the first input :

<input type="radio" name="test" <?php echo $author === 0 ? 'checked' : '' ?> />

The HTML markup is wrong. With that HTML the user is able to select all of those radios. To make it so that the user can only select one of the radio buttons out of that group of options, change the names to all match, like this:

<input type="radio" name="test1" <?PHP echo $checked; ?> />
<input type="radio" name="test1" <?PHP echo $checked; ?> />
<input type="radio" name="test1" <?PHP echo $checked; ?> />

Something you can do:

<?php
    $checked = ($author >= 0 && $author <= 2 ? 'checked' : '');
?>

HTML

<input type="radio" name="test" <?php echo $checked; ?> />
<input type="radio" name="test1" <?php echo $checked; ?> />
<input type="radio" name="test2" <?php echo $checked; ?> />
<?php
    switch($author) {
        case 0:
        case 1:
        case 2:
             $checked = "checked";
             break;
        default:
             $checked = '';
    }

Ideally you want to check a button if the author value relates to that input, for example:

<input type="radio" name="test" value="0" <?php echo ($author == 0 ? 'checked="checked"' : ''); ?> />
<input type="radio" name="test" value="1" <?php echo ($author == 1 ? 'checked="checked"' : ''); ?> />
<input type="radio" name="test" value="2" <?php echo ($author == 2 ? 'checked="checked"' : ''); ?> />

Your currently checking all if the value is any.