简体   繁体   中英

PHP: why my switch always picks the same value?

function getValue($v) {
        $b = 22;
        switch ($v) {
            case ($v <= $b * 1.2):
                $v1 = $b;   break;
            case ($v1 > $b * 1.2 && $v1 <= $b * 2.2):
                $v1 = $b * 2;   break;
            case ($v1 > $b * 2.2 && $v1 <= $b * 3.2):
                $v1 = $b * 3;   break;
            case ($v1 > $b * 3.2):
                $v1 = $b * 4;   break;
            default:
                $v1 = $b;
        }
        return $v1;
    }

the getValue(25) or getValue(45) always returns 22.

I don't think you could use switch like that. It needs values. It does not accept boolean conditions. Check here http://php.net/manual/en/control-structures.switch.php

function getValue($v) {
        $b = 22;
        switch ($v) {
            case ($v <= $b * 1.2):
                $v1 = $b;   break;
            case ($v > $b * 1.2 && $v <= $b * 2.2):
                $v1 = $b * 2;   break;
            case ($v > $b * 2.2 && $v <= $b * 3.2):
                $v1 = $b * 3;   break;
            case ($v > $b * 3.2):
                $v1 = $b * 4;   break;
            default:
                $v1 = $b;
        }
        return $v1;
    }

Your using $v1 instead of $v wrongly.

Above code will work.

You're checking the wrong variable, so you should replace $v1 with $v in the case statements throughout. But also, switch statements are designed to accept constant values, so you'd be better to replace them all with if .

In addition, parts of your expressions are redundant because you've already checked for them. After checking that $v <= $b * 1.2 and finding it isn't, there's no need to check that $v > $b * 1.2 in the next expression. Your default clause can never be matched because all values of $v are <= $b * 3.2 or > $b * 3.2 .

function getValue($v) {
    $b = 22;
    if ($v <= $b * 1.2) {
        return $b;
    } elseif ($v <= $b * 2.2) {
        return $b * 2;
    } elseif ($v <= $b * 3.2) {
        return $b * 3;
    } else {
        return $b * 4;
    }
}

With a switch , you test the given value against the cases.

So your statement does $v == ($v <= $b * 1.2) , $v == ($v1 > $b * 1.2 && $v1 <= $b * 2.2) , etc.

You can do a switch and test against true :

function getValue($v) {
    $b = 22;
    switch (true) {
        case ($v <= $b * 1.2):
            $v1 = $b;   break;
        case ($v1 > $b * 1.2 && $v1 <= $b * 2.2):
            $v1 = $b * 2;   break;
        case ($v1 > $b * 2.2 && $v1 <= $b * 3.2):
            $v1 = $b * 3;   break;
        case ($v1 > $b * 3.2):
            $v1 = $b * 4;   break;
        default:
            $v1 = $b;
    }
    return $v1;
}

However in this case an if / else seems like the better choice:

function getValue($v) {
    $b = 22;
    if ($v <= $b * 1.2) {
        $v1 = $b;
    } elseif ($v1 > $b * 1.2 && $v1 <= $b * 2.2) {
        $v1 = $b * 2;
    } elseif ($v1 > $b * 2.2 && $v1 <= $b * 3.2) {
        $v1 = $b * 3;
    } elseif ($v1 > $b * 3.2) {
        $v1 = $b * 4;
    } else {
        $v1 = $b;
    }

    return $v1;
}

Look at your case labels: they are the results of relational operations, so all of them evaluate to either true or false! %v will be compared to these Boolean values. That's why you're observing such similar output for your input cases.

Replace the switch with an if block and all will be well. And do review your use of $v1 ; did you mean that?

in your switch I can see variable $v1 in the second and third and fourth case so think of the condition which we don't go through the first case so what is the variable $v1 then ? it is not set yet so it goes to the case default or first case always.

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