Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 575
James Andino Avatar asked Dec 13 '22 19:12

James Andino


1 Answers

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.

like image 80
Peter Avatar answered Dec 27 '22 09:12

Peter