【问题标题】:php switch first case not working properlyphp切换第一种情况无法正常工作
【发布时间】:2015-04-28 04:48:24
【问题描述】:

我有这个功能,当$time = 0

时执行第一个案例
    function time_spended($time){
        switch($time){
            case $time > (60*60*24*365):
                $time /= (60*60*24*365);
                return number_format($time, 2, '.', ',') . " year" . ($time > 1 ? "s":"");
                break;
            case $time > 60*60*24:
                $time /= 60*60*24;
                return number_format($time, 2, '.', ',') . " day" . ($time > 1 ? "s":"");
                break;
            case $time > 60*60:
                $time /= 60*60;
                return number_format($time, 2, '.', ',') . " hour" . ($time > 1 ? "s":"");
                break;
            case $time > 60:
                $time /= 60;
                return number_format($time, 2, '.', ',') . " minute" . ($time > 1 ? "s":"");
                break;
            default:
                return number_format($time, 2, '.', ',') . " seconds";
        }
    }

例如:

echo time_spended(0); // 0.00 year

代替:

0.00 秒

【问题讨论】:

  • 只是提醒一下,但所有内容都将被添加为复数,因为 $time 总是大于 1

标签: php function switch-statement conditional-statements


【解决方案1】:

函数返回0.00 year换句话说,是第一个case的结果),因为switch中的$time = 0计算为false,并且$time > (60*60*24*365)是@ 987654329@ 它从第一个分支返回结果,

[0 == true] => [false == true] => [false]

为了让它工作,你应该使用switch(true)而不是switch($time),它应该像下面显示的那样工作:

[true == (0 > 60*60*24*365)] => [true == true] => [false]

Example

【讨论】:

  • 谢谢,我不知道。
  • 我倾向于同意 Cristik 的回答,即在这种情况下,switch 不是适合这项工作的工具。
  • @Mike:我可能错了,但在 php 文档中应该是这样的 switch 使用示例。无论如何,这完全取决于一个人的选择或/和编码风格。
  • @notulysses 在switch page 上有几个使用switch(true) 的cmets,但实际文档内容中没有。但我同意,这是个人编码风格的问题。如果我在代码中看到它,我只是倾向于做双重考虑。 if/elseif/else 在这种情况下对我来说似乎更具可读性。
【解决方案2】:

switch 中的条件应放在括号中。

function time_spended($time){
    switch($time){
        case ($time > (60*60*24*365)):
            $time /= (60*60*24*365);
            return number_format($time, 2, '.', ',') . " year" . ($time > 1 ? "s":"");
            break;
        case ($time > 60*60*24):
            $time /= 60*60*24;
            return number_format($time, 2, '.', ',') . " day" . ($time > 1 ? "s":"");
            break;
        case ($time > 60*60):
            $time /= 60*60;
            return number_format($time, 2, '.', ',') . " hour" . ($time > 1 ? "s":"");
            break;
        case ($time > 60):
            $time /= 60;
            return number_format($time, 2, '.', ',') . " minute" . ($time > 1 ? "s":"");
            break;
        default:
            return number_format($time, 2, '.', ',') . " seconds";
    }
}

【讨论】:

  • 那个开关总是转到default 分支,括号根本没有帮助
  • 原因是 $time 被作为 0 传入,这也等同于 false。第一条语句返回 false,因为$time 小于 31 百万,所以它被执行了。
【解决方案3】:

您没有正确使用switch 语句。 switch 语句用于在多个值中进行选择。更合适/更清晰的方法是使用elif's 实现逻辑:

function time_spended($time){        
        if($time > (60*60*24*365)) {
            //...
        } elseif ($time > 60*60*24) {
            // ...
        } elseif($time > 60*60) {
            // ...
        } elseif($time > 60) {
            //...
        } else {
            return number_format($time, 2, '.', ',') . " seconds";
        }          
}

switch 的正确示例是:

switch($currentDayOfTheWeek){
    case 'Monday': // ...
    case 'Tuesday': // ...
    case 'Wednesday: // ...
    // and so on
    default: // cause it's goo practice to have a default  branch
}

编辑:感谢 Mike 的 elif 评论 :)

【讨论】:

  • 注意 PHP 使用 elseif 而不是 elif
  • 糟糕...语言混淆:P。感谢您的注意,将更新我的答案。
猜你喜欢
  • 2014-11-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多