本文介绍了条件切换的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下示例摘自 http://php.net/manual/de/control-structures.switch.php

<?php
$totaltime = 0;
switch ($totaltime) {

    case ($totaltime < 1):
        echo "That was fast!";
        break;

    case ($totaltime > 1):
        echo "Not fast!";
        break;

    case ($totaltime > 10):
        echo "That's slooooow";
        break;
}

?>

我预期的结果是太快了".但实际结果是不快!".如果有人能解释我为什么会很好?

I expected the result as "That was fast." But actual result is "Not fast!". It would be great if some one can explain me why?

但是如果我添加另一个案例,case 0: echo "那太快了!". 然后它正确地回显了.即那太快了!".请帮助我如何使用条件 switch 语句.

But if i add another case, case 0: echo "That was super fast!". Then it is echoing properly. i.e "That was super fast!". Please help me how to use conditional switch statement.

-

感谢大家的回复.通过将ong switch($totaltime)修改为switch(1)

Thanks all for your responses. I am able to overcome the above problem by modifyong switch($totaltime) to switch(1)

推荐答案

case ($totaltime < 1): 对 PHP 来说意味着 1 (那个方程返回真)

case ($totaltime < 1): means 1 to PHP (that equation returns true)

case ($totaltime > 1): 表示 0 到 PHP(等式返回 false)

case ($totaltime > 1): means 0 to PHP (that equation returns false)

因为 $totaltime 是 0,你得到那个输出

Since $totaltime is 0, you get that output

换句话说,PHP 将 $totaltime 与比较的结果进行比较.

In other words PHP compares $totaltime to the result of the comparisons.

关于 OP 中的 EDIT 的

EDIT regarding EDIT in OP:

你需要去掉 switch() 语句.您只使用它来轻松地与不同的值进行比较,而不使用其他表达式.

You need to get rid of the switch()-statement. You only use it to easily compare against different values and not use additional expressions with it.

我的意思是有什么问题

<?php
$totaltime = 0;

if ($totaltime < 1) {
    echo "That was fast!";
} else if ($totaltime > 10) {
    echo "That's slooooow";
} else if ($totaltime > 1) {
    echo "Not fast!";
}

?>

请注意,我切换了最后两个 if 语句以使其真正起作用.

please note that I switched the last two if-statements to make it really work.

这篇关于条件切换的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 14:58