更新:您有语法错误。)
在第一个末尾缺少 a if
。
两种简单的方法:
// first condition should be by itself as it's a terminal error
if(empty($_POST['a']) or !in_array($_POST['a'], array('1', '2'))) {
echo 'error1';
die; // or redirect here or just enfore a default on $_POST['a'] = 1; // let's say
}
// Second can be like this or embraced in the else of the first one (se ex.2)
if ($_POST['a'] == '1') {
// do something;
} else if ($_POST['a'] == '2') {
// do something;
}
或者
// first condition should be by itself as it's a terminal error
if(empty($_POST['a']) or !in_array($_POST['a'], array('1', '2'))) {
echo 'error1';
// or redirect here or just enfore a default on $_POST['a'] = 1; // let's say
}else{ // Second is in the else here :)
if ($_POST['a'] == '1') {
// do something;
} else if ($_POST['a'] == '2') {
// do something;
}
}
你的最后一个else
不会到达,因为它总是if
在你处理空虚和非法值的第一个结束。