Switch case with three parameters?

前端 未结 7 1542
野趣味
野趣味 2020-12-17 20:49

Is it possible to use three parameters in switch-case, like this:

switch($var1, $var2, $var3){
    case true, false, false:
        echo \"Hello\";
        b         


        
相关标签:
7条回答
  • 2020-12-17 21:24

    You don't have a switch situation here. You have a multiple condition:

    if($var && !($var2 || $var3)) { ...
    
    0 讨论(0)
  • 2020-12-17 21:25

    I don't know - if you really want it this way - maybe cast them all to string, concatenate and then use the resulting string in your case condition?

    0 讨论(0)
  • 2020-12-17 21:26

    Another option is to create a function that maps three parameters to an integer and use that in the switch statement.

    function MapBool($var1, $var2, $var3){
        // ...
    }
    
    switch(MapBool($var1, $var2, $var3)) {
        case 0: 
            echo "Hello";
            break;
       // ...
    
    }
    
    0 讨论(0)
  • 2020-12-17 21:27

    I would just use the if/else

    if($var1 == true && $var2 == false && $var3 == false){
        echo "Hello";
    }
    

    or

    if($var1 && !($var2 && $var3)) {
        echo "Hello";
    }
    
    0 讨论(0)
  • 2020-12-17 21:30

    The syntax is not correct and I wouldn't recommend it, even if it was. But if you really want to use a construct like that, you can put your values into an array:

    switch (array($var1, $var2, $var3)) {
        case array(true, false, false):
            echo "hello";
            break;
    }
    
    0 讨论(0)
  • 2020-12-17 21:32

    i don't think your syntax is valid.

    I'd nest the switch statements.

    0 讨论(0)
提交回复
热议问题