Prospero Prospero - 1 month ago 18
PHP Question

php switch case problem

I am trying to say

$level > -100 && $level < 100


$level = 0;

switch($level){

case $level > -100:
break;
case $level < 100:
break;
default:
echo '5';
return null;
}


can you use a switch statement like this.

Answer

None of the answers presented so far have explicitly connected the spirit of the original question with a proper switch construction. So, for the record:

switch (true) {
    case (($level>-100) && ($level<100)):
        echo 'in range one';
        break;
    case (($level>200) && ($level<300)):
        echo 'in range two';
        break;
    default:
        echo 'out of range';
}

There's absolutely nothing wrong with this usage of switch.