简体   繁体   中英

php switch first case not working properly

I have this function where first case is executed when $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";
        }
    }

For example:

echo time_spended(0); // 0.00 year

Instead of:

0.00 seconds

Function returns 0.00 year ( in other words, result from the first case ), because $time = 0 in switch evaluates to false , and as $time > (60*60*24*365) is true it returns result from the first branch, viz :

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

To make it work you should use switch(true) instead of switch($time) , it should work the way it is shown below:

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

Example

Conditions in switch should be in brackets.

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";
    }
}

You are not using the switch statement correctly. A switch statement is used to pick among several values. A more suitable/clear approach would be to implement the logic with 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";
        }          
}

A correct example of switch would be:

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

Edit: Thanks Mike for the elif comment :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM