Switch doesn't work with numeric comparison cases

后端 未结 4 1792
滥情空心
滥情空心 2020-12-07 03:44

Here is my code.

$value = 0;
switch($value) {
      case ( $value <= 25 ):
            $CompScore = \'low\';
            break;
      case ($value > 25         


        
4条回答
  •  暖寄归人
    2020-12-07 04:16

    The problem is, you use your switch in a particular way.

    You are saying :

    $value = 0;
    Switch ($value){
        case ($value < 25):
            ....
    
    }
    

    This finally compares $value<25 and 0 as described below :

    ($value<25) == $value.
    => true == 0
    

    Which is wrong because true != 0

    A way to do what you want this way is simply to replace switch($value) with switch(true) so the interpreter will actually compare check if your case statements are true.

提交回复
热议问题