Switch case with three parameters?

前端 未结 7 1543
野趣味
野趣味 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:36

    This is the sort of thing that used to be handled by bitwise operators:

    if (($var1 << 2) & ($var2 << 1) & $var3) == 4) ...
    

    ...back when 'true' was 1.

    That being said, the above is concise, but it's pretty hard to read and maintain. Nevertheless, if you have a lot of similar statements, shifting/ANDing might be a way to go to get things under control:

    switch (($var1 << 2) & ($var2 << 1) & $var3)) {
       case 0: // false, false, false
           ...stuff...
       case 1: // false, false, true
           ...different stuff...
       // all 8 cases if you REALLY care
    }
    
    0 讨论(0)
提交回复
热议问题