简体   繁体   English

PHP:为什么我的交换机总是选择相同的值?

[英]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. getValue(25)getValue(45)始终返回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 在这里检查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. 您错误地使用$v1而不是$v

Above code will work. 上面的代码将起作用。

You're checking the wrong variable, so you should replace $v1 with $v in the case statements throughout. 您正在检查错误的变量,因此在整个case语句中应将$v1替换$v1 $v But also, switch statements are designed to accept constant values, so you'd be better to replace them all with if . 而且, switch语句被设计为接受常量值,因此最好将它们全部替换为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. 在检查了$v <= $b * 1.2并发现它不是之后,无需在下一个表达式中检查$v > $b * 1.2 Your default clause can never be matched because all values of $v are <= $b * 3.2 or > $b * 3.2 . 您的default子句永远无法匹配,因为$v 所有值均<= $b * 3.2> $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. 使用switch ,您可以根据案例测试给定的值。

So your statement does $v == ($v <= $b * 1.2) , $v == ($v1 > $b * 1.2 && $v1 <= $b * 2.2) , etc. 因此,您的语句确实执行$v == ($v <= $b * 1.2)$v == ($v1 > $b * 1.2 && $v1 <= $b * 2.2)等。

You can do a switch and test against true : 您可以进行切换并测试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: 但是在这种情况下, if / else似乎是更好的选择:

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! 看一下您的案例标签:它们是关系运算的结果,所以它们的总和为true或false! %v will be compared to these Boolean values. %v将与这些布尔值进行比较。 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. if块替换switch ,一切都会好起来。 And do review your use of $v1 ; 并查看您对$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 ? 在您的开关中,我可以在第二,第三和第四种情况下看到变量$ v1,因此请考虑一下我们不通过第一种情况的情况,那么变量$ v1呢? it is not set yet so it goes to the case default or first case always. 它尚未设置,因此总是默认情况或第一种情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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