切换箱有条件

时间:2014-10-20 19:33:56

标签: php switch-statement

这里总是显示"那很快!"为什么是这样?为什么输出不应该是"那个懒散的"?

$totaltime = 12;
switch ($totaltime<=13) {

case 1:
echo "That was fast!";
break;

case 5:
echo "Not fast!";
break;

case 12:
echo "That's slooooow";
break;

case 15:
echo "That's too slooooow";
break;
}

5 个答案:

答案 0 :(得分:1)

您正在启用此功能:

$totaltime<=13

这是真的,所以它来到1,而1==true是真的,所以它'触发'。

你的意思不是吗?

switch ($totaltime) {

case 1:
echo "That was fast!";
break;

case 5:
echo "Not fast!";
break;

case 12:
echo "That's slooooow";
break;

case 15:
echo "That's too slooooow";
break;
}

答案 1 :(得分:1)

($ totaltime&lt; = 13)被评估为1,这就是为什么你在第一种情况下结束,将代码更改为:

$totaltime = 12;
switch ($totaltime) {

case 1:
echo "That was fast!";
break;

case 5:
echo "Not fast!";
break;

case 12:
echo "That's slooooow";
break;

case 15:
echo "That's too slooooow";
break;
}

答案 2 :(得分:1)

你正在使用一个条件用于switch语句。它应该是这样的:

$totaltime = 12;
switch ($totaltime) {

case 1:
echo "That was fast!";
break;

case 5:
echo "Not fast!";
break;

case 12:
echo "That's slooooow";
break;

case 15:
echo "That's too slooooow";
break;
}

答案 3 :(得分:1)

这将有效:

   $totaltime = 12;
    switch ($totaltime) {

case 1:
echo "That was fast!";
break;

case 5:
echo "Not fast!";
break;

case 12:
echo "That's slooooow";
break;

case 15:
echo "That's too slooooow";
break;
}

答案 4 :(得分:1)

我不确定,但我认为($ totaltime&lt; = 13)等于true或1.你应该只使用$ totaltime。