Switch doesn't work with numeric comparison cases

Here is my code.

$value = 0;
switch($value) {
      case ( $value <= 25 ):
            $CompScore = 'low';
            break;
      case ($value > 25 && $value <= 50 ):
            $CompScore = 'fair';
            break;
      case ($value > 50 && $value <= 75 ):
            $CompScore = 'good';
            break;
      case ($value >75 ):
            $CompScore = 'excellent';
            break;
      default:
            $CompScore = 'low';
            break;
}

echo $CompScore;

When the value is 0, $compScore is fair. Why it is not showing low? I don’t understand why.

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

switch not working like that.

Since $value is 0 which is falsy value.

$value <= 25 is true, $value > 25 && $value <= 50 is false, so $CompScore will be 'fair'.

For you code, use an if elseif else flow will be more readable.

You could rewrite your code like below:

// from excellent to low
if ($value > 75) {
  $CompScore = 'excellent';
} else if ($value > 50) {
  $CompScore = 'good';
} else if ($value > 25) {
  $CompScore = 'fair';
} else {
  $CompScore = 'low';
}

Solution 2

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.

Solution 3

The value you pass into a switch statement is basically what the switch statement looks for an evaluated match for, going from top to bottom down the list of cases until it finds one it is equal to (loose comparison), e.g. true == true.

In your example, your comparisons are evaluated as booleans (true or false) – your variable $value is set to zero, which is equal to false, but not identical (strict comparison) to false. For example:

(0 == false)    // true
(0 === false)   // false
(1 == false)    // false
(1 === false)   // false
(1 == true)     // true
(1 === true)    // false
(true === true) // true

So by using a boolean true as your switch value, you can do this to have numeric comparison inside the statement, where each comparison will evaluate to either true or false to match/not match the original true value (boolean – boolean comparison).

switch(true) {
       case ($value <= 25):                 // true
             $CompScore = 'low';
             break;
       case ($value > 25 && $value <= 50 ): // false
             $CompScore = 'fair';
             break;
       case ($value > 50 && $value <= 75 ): // false
             $CompScore = 'good';
             break;
       case ($value >75 ):                  // false
             $CompScore = 'excellent';
             break;
       default:                             // if you removed the first case
             $CompScore = 'low';            // this default case would be used
             break;                         
 }

Solution 4

You can “smarten up” your code with a lookup array and some simple mathematics.

Code: (Demo)

$lookup = [
    1 => 'low',
    2 => 'fair',
    3 => 'good',
    4 => 'excellent'
];

for ($i = -1; $i <= 101; ++$i) {
    echo "\n$i : " , $lookup[ceil($i / 25)] ?? $lookup[1];
}

By dividing the value by 25, then rounding up to the next whole number (with ceil()), you achieve the same result without a battery of condition statements. Using a lookup away not only keeps your code compact, it ensures that you aren’t performing n checks on the same value AND it provides a clean separation between the process and the values.

If you ever decide to split the groups by 20, instead of 25, you would only need to change the 25 to 20, then add a fifth lookup value in the desired location (with the appropriate key).

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply