PHP单选按钮组变量

PHP radio button group variables

我已经简化了这个问题。但是我需要做的就是在选中按钮组合时显示不同的结果。目前如果检查 Rice 和 Tea 没有结果。

<?PHP

if (isset($_POST['Submit1'])) {

    $selected_radio = $_POST['food']."|".$_POST['drink'];

        if ($selected_radio == 'rice' && 'tea') {
            print '<h1>Rice and tea</h1>';

        }
        else if ($selected_radio == 'beans' && 'coffee') {
            print '<h1>Beans and coffee</h1>';
        }
}

?>  


<form name ="form1" method ="POST" action =" ">

<p><input type = 'Radio' Name ='food'  value= 'rice'>Rice</p>

<p><input type = 'Radio' Name ='food'  value= 'beans'>Beans</p>


<p><input type = 'Radio' Name ='drink'  value= 'tea'>Tea</p>

<p><input type = 'Radio' Name ='drink'  value= 'coffee'>Coffee</p>


<input type = "Submit" Name = "Submit1"  value = "Select">
</form>

(很抱歉之前问了类似的问题)

如果条件更新

if (isset($_POST['Submit1'])) {

    $selected_radio = $_POST['food']."|".$_POST['drink'];

        if ($selected_radio == 'rice|tea') {
            print '<h1>Rice and tea</h1>';

        }
        else if ($selected_radio == 'beans|coffee') {
            print '<h1>Beans and coffee</h1>';
        }
}

更改 if 的条件:

    if (strpos($selected_radio, 'rice') !== FALSE && strpos($selected_radio, 'tea') !== FALSE) {
        print '<h1>Rice and tea</h1>';

    }
    else if (strpos($selected_radio, 'beans') !== FALSE && strpos($selected_radio, 'coffee') !== FALSE) {
        print '<h1>Beans and coffee</h1>';
    }

(这只是一个建议,我没有测试过;))

使用此代码:

$selected_radio_list = [
    'rice|tea' => 'foo',
    'beans|coffee' =>'bar'
];  

$post = $_POST['food']. "|" .$_POST['drink'];  

if(in_array($post, $selected_radio_list )){
    echo $selected_radio_list[$post];
}