SWITCH STATEMENT: check multiple conditions using AND operator?

I was curious if it was possible to have a ‘compound case’ (to send multiple variables to be compared) in a switch statement? Something along the lines of:


switch ($var1,$var2,$var3){

case ($var1 && !$var2):
//do stuff 
break;
case ($var2 && !$var1):
//do stuff 
break;
case ($var3 && ( $var1 ||$var2):
//do stuff 
break;
default:
//do nothing


}

of course I cant seem to pass more than one argument in a switch statement. Is this jsut a limitation of the switch statement?

Is this anything like what you are trying to do?



#================
function index()
{
    for( $x=-1; $x<8; $x++):
      echo sprintf('%d   %s', $x, $this->switch_test( $x )) ;
      echo '<br /><br />';
    endfor;
}

#================
function switch_test($val)
{
  $result = '';
  switch(TRUE)
  {
    case ($val>=2 && $val <= 4) :   $result = ' A: ($val>=2 && $val <= 4)';# .$val; # 2 3 4
    break;
    case ($val>=2 && $val <= 5)  :  $result = ' B: ($val>=2 && $val <= 5)'; # 5
    break;
    case ($val>=2 && $val <= 1)  :  $result = ' C: ($val>=2 && $val <= 1)';
    break;
    case ($val>=3 && $val <= 6)  :  $result = ' D: ($val>=3 && $val <= 6)'; # 6
    break;
    default: $result = 'default: ';# .$val;
  }

  return $result;
}


#Output

-1 default:

0 default:

1 default:

2 A: ($val>=2 && $val <= 4)

3 A: ($val>=2 && $val <= 4)

4 A: ($val>=2 && $val <= 4)

5 B: ($val>=2 && $val <= 5)

6 D: ($val>=3 && $val <= 6)

7 default:

Actually, am not trying to do anything specifically.

I have heard it said that switch is an alternative to using sequences of ifelse(){} ; so that rather than writing
ifelse{}
ifelse{}
ifelse{}
ifelse{}
else{}
one writes
switch(){
case:
break;
case:
break;
case:
break;
case:
break;
default;
break;
}

however, may ifelse statements have compound arguments with varied logic?

for example:
if ($a && $b && $c){}
elseif($a && $b){}
elseif($a && $c){}
elseif($b && $c){}
elseif($b || $c){}
elseif($a || $c){}

there is no way to directly convert that to a switch statement is there? ( i noted in your example you didnt place ($val) directly into switch()… but that you still limited yourself different values of ONE argument where as my if statements deal with multiple values of multiple arguments.

Try this:



#===================
function index() #_Dresden()
{
  $x = '$a=%d   $b=%d   $c=%d  Result== %s';

  $a=1; $b=1; $c=1;
  echo sprintf($x, $a, $b, $c, $this->test_again( $a, $b, $c )) ;
  echo  '<br /><br />';

  $a=1; $b=1; $c=0;
  echo sprintf($x, $a, $b, $c, $this->test_again( $a, $b, $c )) ;
  echo  '<br /><br />';

  $a=1; $b=0; $c=0;
  echo sprintf($x, $a, $b, $c, $this->test_again( $a, $b, $c )) ;
  echo  '<br /><br />';

  $a=0; $b=1; $c=1;
  echo sprintf($x, $a, $b, $c, $this->test_again( $a, $b, $c )) ;
  echo  '<br /><br />';

  $a=0; $b=1; $c=0;
  echo sprintf($x, $a, $b, $c, $this->test_again( $a, $b, $c )) ;
  echo  '<br /><br />';

  $a=0; $b=0; $c=1;
  echo sprintf($x, $a, $b, $c, $this->test_again( $a, $b, $c )) ;
  echo  '<br /><br />';

  $a=0; $b=0; $c=0;
  echo sprintf($x, $a, $b, $c, $this->test_again( $a, $b, $c )) ;
  echo  '<br /><br />';

}

#===================
function test_again($a, $b, $c)
{
  $result = '';
  switch(TRUE)
  {
    case ($a && $b && $c): $result = '$a && $b && $c';
    break;
    case ($a && $b):       $result =  '$a && $b';
    break;
    case ($a && $c):       $result =  '$a && $c';
    break;
    case ($b && $c):       $result =  '$b && $c';
    break;
    case ($b || $c):       $result =  '$b || $c';
    break;
    case ($a || $c):       $result =  '$a || $c';
    break;
    default: $result =  'default';
  }

  return $result;
}


#Output

$a=1 $b=1 $c=1 Result== $a && $b && $c

$a=1 $b=1 $c=0 Result== $a && $b

$a=1 $b=0 $c=0 Result== $a || $c

$a=0 $b=1 $c=1 Result== $b && $c

$a=0 $b=1 $c=0 Result== $b || $c

$a=0 $b=0 $c=1 Result== $b || $c

$a=0 $b=0 $c=0 Result== default

Thanks, John

So, what am getting here is that… I dont need to use the $vars within the parenthesis in the switch statement? :slight_smile:

Try and put the $vars in the switch parenthesis statement and see what happens…

entering multiple arguments on a switch stament gets you: Parse error: syntax error, unexpected ‘,’…

I just didn’t know that the switch statement continued to evaluate external arguments… setting switch(TRUE) is a useful trick… thank you.

Just noticed a possible GOTCHA:



$a = "abc";
$b = "def";
# $c = BEWARE NOT DEFINED and NO ERROR THROWN

switch($c)
{
    case "a":
        echo "a";
        break;
    case "b":
        echo "b";
        break;
    default:
        echo "default";
        break;
}


Will output: default when $c is NOT DEFINED