是否可以在PHP中使用多条件语句(开关)导致多条件(可执行代码)?代码可能像这样:
在$fifth
= 9的情况下,$fifth -= $fifth;
= 9, $fourth
= 9, $third
= 9。
switch ($fifth xor $fourth xor $third) {
case '9':
$fifth -= $fifth;
$fourth -= $fourth;
$third -= $third;
break;
default:
$fifth = $fifth;
$fourth = $fourth;
$third = $third;
}
不行,你不能那样做。您必须使用单独的if
语句:
if ($fifth == 9) {
$fifth -= $fifth;
}
if ($fourth == 9) {
....
顺便说一下,$fifth -= $fifth
等于$fifth = 0
,这样更高效和可读。
你可以用这个小玩意
function isOneOf (){
$args = func_get_args();
$test = array_shift($args);
return in_array($test, $args);
}
像这样使用:
if (isOneOf(9, $fifth, $forth, $third)){
// code if one parameters equals 9 (except the first)
}
作为
的替代if ($fifth == 9 || $forth == 9 || $third == 9){
... code
}