views:

130

answers:

8

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.

A: 

No you can't. Switch does only 'equals' type comparison.

Mchl
Feeling better now?
Mchl
+1 because it's definatly not -1
nikic
A: 

No, you can't. The switch statement needs literals in the case blocks. Use an if statements instead:

if(!($level > -100 && $level < 100))
{
  echo '5';
  return null;
}
Femaref
how can level be less than -100 and more than 100?
David_001
meh, wrong rollback. thanks
Femaref
+3  A: 

When you say switch ($level) you're already comparing the value of $level. Each case can then only check for equality, you can't do comparisons like in your example. You'll have to use an if statement instead:

if ($level > -100 && $level < 100)
  ; // do nothing; equivalent of break in this case
else
  echo '5';

Even simpler, just negate the conditions:

if ($level <= -100 || $level >= 100)
  echo '5';
casablanca
A: 

No. Switch statements can only use hard values. You need to convert this to an if statement:'

if($level > -100)
{

}
else if($level < 100)
{

}                                                                                      
else
{
     echo '5';
     return null;                                              
}   
Tim Cooper
Notice that this is equal to `if($level > -100) {} else {}`
Mchl
A: 

This is one of the reasons people advocating case as a superior solution to if-else are off base. I don't like the syntax or the limitations - if-ifelse-else is much more useful.

Mark Snidovich
this doesn't help with the question at all.
Femaref
Neither one is "superior" to the other; each has its own uses. In this case, an `if` statement wins.
casablanca
Right tools for right job...
Mchl
my apples can beat up your oranges.
Doodle
It's just argumentive. Both statements have their merits.
Femaref
I said 'I don't like' which is clearly meant to be known as an opinion. I guess my answer should have been left as a comment, sorry. It's a fact that `switch` is more limited, and that it has a syntax different than other control structures, including the oft-missed `break` (which can cause problems). This isn't apples and oranges, to compare a control structure. `if` is clearly superior overall, since it can do the same thing and a lot more. I'd like to see which one you could do entirely without.
Mark Snidovich
+2  A: 

The other answers are both correct and incorrect at the same time. Incorrect, in that it is possible to do what you want in PHP... change switch($level) to switch(true) and your example will work. Correct, in that it's bad form and if any other programmers see that in your code they'll probably come after you with pitchforks. Its not how the switch statement is intended to be used, and wouldn't work like that in most other languages.

Timothy
I think thats interesting. The fact you can set the switch to true and evaluate for it is clearly intentional. It also when you think about it makes perfect sense. What I would not call it is intuiative.Alas it does seem a case resulting in raptors falling from the sky.
Doodle
A: 

Easy Dude

switch($level) {
      case (($level > -100) && ($level < 100)):
            //Do your stuff
      break;
}

COPY AND PASTE THIS TO CHECK THIS

<?
$level = 50;
switch($level) {
    case (($level>-100) && ($level<100)):
            echo "correct";
        break;
    }
?>
Starx
Now go and try that
Mchl
it is worth noting you dont note why its working and could easily confuse my confused self even more. I bet you got yelled at in math to show your work to.
Doodle
@Doodle, Now you are confusing me.....
Starx
Mchl
Exactly as Mchl states, your code works but it shows that you do not understand exactly what you are doing. Please read the PHP manual entry for the switch statement and comment on what you have learned.
Matthew Purdon
@Mathhew Purdon, I know that this is not the proper use of Switch, but I am just giving the OP what he wants. @mch, then add another condition for exceptional `0`s
Starx
Mchl
@Mchl, I dont think its a buggy solution(it might look buggy is this case), but the ability to compare multiple cases can come very handy in some cases.
Starx
You don't get it, do you? Did you try writing a solution that would fix the bug? This is is just wrong on so many levels. If you really want to use switch in this manner, put `true` into switch, just like other people here suggested.
Mchl
+1  A: 

Apart of if/else, another way to do it:

switch (true)                                                                               
    case $level > -100:
        break;
    case $level <  100:
        break;
    default:
        echo '5';
        return null;
}
nikic
This isn't really same as `$level > -100 ` line will never be reached.
Mchl