如何在PHP中使用switch case 'or'?

262

有没有办法在 PHP switch 语句中使用“OR”运算符或等效的方式?

例如,像这样:

switch ($value) {

    case 1 || 2:
        echo 'the value is either 1 or 2';
        break;
}
12个回答

-2

http://php.net/manual/en/control-structures.switch.php Example

$today = date("D");

    switch($today){

        case "Mon":

        case "Tue":

            echo "Today is Tuesday or Monday. Buy some food.";

            break;

        case "Wed":

            echo "Today is Wednesday. Visit a doctor.";

            break;

        case "Thu":

            echo "Today is Thursday. Repair your car.";

            break;

        default:

            echo "No information available for that day.";

            break;

    }

1
default情况下不需要break - mickmackusa

-4

最好的方法可能是使用if else和请求。此外,这样做可以更容易和清晰地使用。

例如:

<?php 
$go = $_REQUEST['go'];
?>
<?php if ($go == 'general_information'){?>
<div>
echo "hello";
}?>

不要使用那些在 PHP 中无法正常工作的函数,特别是当你将 PHP 嵌入 HTML 时。


无关的回答。 - Prabhu Nandan Kumar

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接