SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    Community Advisor silver trophybronze trophy
    dresden_phoenix's Avatar
    Join Date
    Jun 2008
    Location
    Madison, WI
    Posts
    2,816
    Mentioned
    34 Post(s)
    Tagged
    2 Thread(s)

    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:

    PHP Code:
    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?

  2. #2
    SitePoint Mentor bronze trophy
    John_Betong's Avatar
    Join Date
    Aug 2005
    Location
    City of Angels
    Posts
    1,883
    Mentioned
    74 Post(s)
    Tagged
    6 Thread(s)
    Is this anything like what you are trying to do?

    PHP Code:

    #================
    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>=&& $val <= 4) :   $result ' A: ($val>=2 && $val <= 4)';# .$val; # 2 3 4
        
    break;  
        case (
    $val>=&& $val <= 5)  :  $result ' B: ($val>=2 && $val <= 5)'# 5
        
    break;  
        case (
    $val>=&& $val <= 1)  :  $result ' C: ($val>=2 && $val <= 1)';
        break;  
        case (
    $val>=&& $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:
    Learn how to be ready for The New Move to Discourse

    How to make Make Money Now with a *NEW* look

    Be sure to congratulate Wolfshade on earning Member of the Month for August 2014

  3. #3
    Community Advisor silver trophybronze trophy
    dresden_phoenix's Avatar
    Join Date
    Jun 2008
    Location
    Madison, WI
    Posts
    2,816
    Mentioned
    34 Post(s)
    Tagged
    2 Thread(s)
    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.

  4. #4
    SitePoint Mentor bronze trophy
    John_Betong's Avatar
    Join Date
    Aug 2005
    Location
    City of Angels
    Posts
    1,883
    Mentioned
    74 Post(s)
    Tagged
    6 Thread(s)
    Try this:

    PHP Code:

    #===================
    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
    Last edited by John_Betong; May 9, 2012 at 00:31. Reason: simplified && spelling not my fortay
    Learn how to be ready for The New Move to Discourse

    How to make Make Money Now with a *NEW* look

    Be sure to congratulate Wolfshade on earning Member of the Month for August 2014

  5. #5
    Community Advisor silver trophybronze trophy
    dresden_phoenix's Avatar
    Join Date
    Jun 2008
    Location
    Madison, WI
    Posts
    2,816
    Mentioned
    34 Post(s)
    Tagged
    2 Thread(s)
    Thanks, John


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

  6. #6
    SitePoint Mentor bronze trophy
    John_Betong's Avatar
    Join Date
    Aug 2005
    Location
    City of Angels
    Posts
    1,883
    Mentioned
    74 Post(s)
    Tagged
    6 Thread(s)
    Try and put the $vars in the switch parenthesis statement and see what happens...
    Learn how to be ready for The New Move to Discourse

    How to make Make Money Now with a *NEW* look

    Be sure to congratulate Wolfshade on earning Member of the Month for August 2014

  7. #7
    Community Advisor silver trophybronze trophy
    dresden_phoenix's Avatar
    Join Date
    Jun 2008
    Location
    Madison, WI
    Posts
    2,816
    Mentioned
    34 Post(s)
    Tagged
    2 Thread(s)
    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.

  8. #8
    SitePoint Mentor bronze trophy
    John_Betong's Avatar
    Join Date
    Aug 2005
    Location
    City of Angels
    Posts
    1,883
    Mentioned
    74 Post(s)
    Tagged
    6 Thread(s)
    Just noticed a possible GOTCHA:

    PHP Code:

    $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
    Learn how to be ready for The New Move to Discourse

    How to make Make Money Now with a *NEW* look

    Be sure to congratulate Wolfshade on earning Member of the Month for August 2014


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •