PHP:在 Switch 的情况下有两个值? [英] PHP: two values in the case of Switch?
问题描述
如果输入car"或ferrari",则应打印car or ferrari".我怎样才能实现它?
If "car" or "ferrari" as an input, it should print "car or ferrari". How can I achieve it?
<?php
$car ='333';
switch($car)
{
case car OR ferrari:
print("car or ferrari");
break;
case cat:
print("cat");
break;
default:
print("default");
break;
}
?>
推荐答案
使用两个 case
子句:
case 'car':
case 'ferrari':
print("car or ferrari");
break;
说明:
了解如何执行 switch
语句以避免错误很重要.switch
语句逐行执行(实际上是逐个语句).一开始,没有执行任何代码.只有当case
语句的值与switch
表达式的值匹配时,PHP 才会开始执行这些语句.PHP 会继续执行语句,直到 switch
块结束,或者第一次看到 break
语句.如果你没有在 case 语句列表的末尾写一个 break
语句,PHP 将继续执行后面 case 的语句.
It is important to understand how the
switch
statement is executed in order to avoid mistakes. Theswitch
statement executes line by line (actually, statement by statement). In the beginning, no code is executed. Only when acase
statement is found with a value that matches the value of theswitch
expression does PHP begin to execute the statements. PHP continues to execute the statements until the end of theswitch
block, or the first time it sees abreak
statement. If you don't write abreak
statement at the end of a case's statement list, PHP will go on executing the statements of the following case.
这篇关于PHP:在 Switch 的情况下有两个值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!