带有条件开关的PHP-Switch case语句
我可以把条件语句放在switch语句中吗。ex-switch($totaltime是的,您可以从PHP的switch文档: switch语句类似于同一表达式上的一系列IF语句。在许多情况下,您可能希望将同一变量(或表达式)与许多不同的值进行比较带有条件开关的PHP-Switch case语句,php,switch-statement,Php,Switch Statement,我可以把条件语句放在switch语句中吗。ex-switch($totaltime是的,您可以从PHP的switch文档: switch语句类似于同一表达式上的一系列IF语句。在许多情况下,您可能希望将同一变量(或表达式)与许多不同的值进行比较 当case具有常量值时,就像说case value==switch value,但是您可以为case使用更复杂的表达式。是的,您可以从PHP的switch文档: switch语句类似于同一表达式上的一系列IF语句。在许多情况下,您可能希望将同一变量(或表
当case具有常量值时,就像说case value==switch value,但是您可以为case使用更复杂的表达式。是的,您可以从PHP的switch文档: switch语句类似于同一表达式上的一系列IF语句。在许多情况下,您可能希望将同一变量(或表达式)与许多不同的值进行比较 当case有常量值时,就像说case value==switch value,但是对于一个case,您可以有更复杂的表达式。是的,您可以(除了switch中的比较)
$totaltime=12;
开关(真){
案例($totaltime是您可以(除了开关内的比较)
$totaltime=12;
开关(真){
case($totaltime开关仅检查第一个条件是否等于第二个条件,这样:
switch (CONDITION) {
case CONDITION2:
echo "CONDITION is equal to CONDITION2";
break;
}
switch (true) {
case $totaltime <= 1: #This checks if true (first condition) is equal to $totaltime <= 1 (second condition), so if $totaltime is <= 1 (true), is the same as checking true == true.
echo "That was fast!";
break;
case $totaltime <= 5:
echo "Not fast!";
break;
case $totaltime >= 10 && $totaltime<=13:
echo "That's slooooow";
break;
}
所以你必须这样做:
switch (CONDITION) {
case CONDITION2:
echo "CONDITION is equal to CONDITION2";
break;
}
switch (true) {
case $totaltime <= 1: #This checks if true (first condition) is equal to $totaltime <= 1 (second condition), so if $totaltime is <= 1 (true), is the same as checking true == true.
echo "That was fast!";
break;
case $totaltime <= 5:
echo "Not fast!";
break;
case $totaltime >= 10 && $totaltime<=13:
echo "That's slooooow";
break;
}
开关仅检查第一个条件是否等于第二个条件,这样:
switch (CONDITION) {
case CONDITION2:
echo "CONDITION is equal to CONDITION2";
break;
}
switch (true) {
case $totaltime <= 1: #This checks if true (first condition) is equal to $totaltime <= 1 (second condition), so if $totaltime is <= 1 (true), is the same as checking true == true.
echo "That was fast!";
break;
case $totaltime <= 5:
echo "Not fast!";
break;
case $totaltime >= 10 && $totaltime<=13:
echo "That's slooooow";
break;
}
所以你必须这样做:
switch (CONDITION) {
case CONDITION2:
echo "CONDITION is equal to CONDITION2";
break;
}
switch (true) {
case $totaltime <= 1: #This checks if true (first condition) is equal to $totaltime <= 1 (second condition), so if $totaltime is <= 1 (true), is the same as checking true == true.
echo "That was fast!";
break;
case $totaltime <= 5:
echo "Not fast!";
break;
case $totaltime >= 10 && $totaltime<=13:
echo "That's slooooow";
break;
}
确切的问题是什么?运算符不能覆盖所有可能的情况?这就像是:@icecub的一个几乎完全相同的副本,但它在switch语句中没有条件。这就是我在这里发布的原因,您不能在switch参数中放入条件。只需在其周围包装一个if语句:if($totaltime我看到了您的个人资料。当您提出问题时,请选择正确的答案作为已接受。在13个有答案的问题中,您只有2个问题的答案为已接受。确切的问题是什么?操作员无法涵盖所有可能的情况?这类似于几乎完全相同的:@icecub,但它在开关状态下没有条件这就是为什么我在这里发布你不能在switch参数中添加条件的原因。只需在它周围包装一个if语句:if($totaltime我看到了你的个人资料。当你提问时,请选择正确的答案作为已接受。在13个有答案的问题中,你只有2个问题的答案是已接受的。$totaltime=12;switch(false){case($totaltime)这是因为您与计算结果为false的第一个case语句匹配。$totaltime$totaltime=12;switch(false){case($totaltime)这是因为您与计算结果为false的第一个case语句匹配。$totaltime