PHP CASE statement not working with ZERO values

php, switch-statement

Solution

Change `switch ($level)` to `switch (true)` and this will work.

`switch` statements perform equality tests on the values in the cases. PHP is evaluating your `>` comparisons, so `case $level > 80` becomes `case false`. `false` is considered to be equal to `0`, so the first case matches.

Problem

I don't understand what's happening here. Logically, it doesn't make any sense to me. ``` <?php $level = 0; switch ($level) { case $level > 80: $answer = 'high'; break; case $level > 60: $answer = 'moderate-to-high'; break; case $level > 40: $answer = 'moderate'; break; case $level > 20: $answer = 'low-to-moderate'; break; default: $answer = 'low'; break; } echo $answer; ?> ``` When $level == 0, it returns "high". This doesn't make any sense to me. Can someone explain what's happening here?

Original source