我可以使用逻辑“或"吗?在 PHP switch 语句的情况下?
问题描述:
是否可以在 switch case 中使用or"或and"?这就是我所追求的:
Is it possible to use "or" or "and" in a switch case? Here's what I'm after:
case 4 || 5:
echo "Hilo";
break;
答
不,但你可以这样做:
case 4:
case 5:
echo "Hilo";
break;
请参阅 PHP 手册.
关于 AND 情况:switch 只检查一个变量,所以这不起作用,在这种情况下你可以这样做:
About the AND case: switch only checks one variable, so this won't work, in this case you can do this:
switch ($a) {
case 4:
if ($b == 5) {
echo "Hilo";
}
break;
// Other cases here
}