switch语句一次有两个变量

时间:2022-10-06 14:20:38

Could someone suggest the best way to have the following switch statement? I don't know that it's possible to compare two values at once, but this would be ideal:

有人可以建议使用以下switch语句的最佳方法吗?我不知道可以一次比较两个值,但这是理想的:

switch($color,$size){
    case "blue","small":
        echo "blue and small";
    break;

    case "red","large";
        echo "red and large";
    break;
}


This could be comparable to:

if (($color == "blue") && ($size == "small")) {
    echo "blue and small";
}
elseif (($color == "red") && ($size == "large")) {
    echo "red and large";
}

Update I realized that I'll need to be able to negate ($color !== "blue") and compare as opposed to equating variables to strings.

更新我意识到我需要能够否定($ color!==“blue”)并进行比较,而不是将变量等同于字符串。

6 个解决方案

#1


26  

You can change the order of the comparison, but this is still not ideal.

您可以更改比较的顺序,但这仍然不理想。

    switch(true)
    {
      case ($color == 'blue' and $size == 'small'):
        echo "blue and small";
        break;
      case ($color == 'red' and $size == 'large'):
        echo "red and large";
        break;
      default:
        echo 'nothing';
        break;
    }

#2


27  

Using the new array syntax, this looks almost like what you want:

使用新的数组语法,这看起来几乎像你想要的:

switch ([$color, $size]) {
    case ['blue', 'small']:
        echo 'blue and small';
    break;

    case ['red', 'large'];
        echo 'red and large';
    break;
}

#3


16  

Doesn't work. You could hack around it with some string concatentation:

不起作用。你可以用一些字符串连接来解决它:

switch($color . $size) {
   case 'bluesmall': ...
   case 'redlarge': ...
}

but that gets ugly pretty quick.

但这很快就变丑了。

#4


7  

Found at http://www.siteduzero.com/forum/sujet/switch-a-plusieurs-variables-75351

发现于http://www.siteduzero.com/forum/sujet/switch-a-plusieurs-variables-75351

<?php
$var1 = "variable1";
$var2 = "variable2";
$tableau = array($var1, $var2);

switch ($tableau){
    case array("variable1", "variable2"):
        echo "Le tableau correspond !";
    break;

    case array(NULL, NULL):
        echo "Le tableau ne correspond pas.";
    break; 
}
?>

#5


1  

Your other option (though not pretty) is to nest the switch statements:

你的另一个选择(虽然不是很漂亮)是嵌套switch语句:

switch($color){
    case "blue":
        switch($size):
            case "small":
            //do something
            break;
    break;
}

#6


0  

var $var1 = "something";
var $var2 = "something_else";
switch($var1.$var2) {
case "somethingsomething_else":
    ...
    break;
case "something...":
    break;
case "......":
    break;
}

#1


26  

You can change the order of the comparison, but this is still not ideal.

您可以更改比较的顺序,但这仍然不理想。

    switch(true)
    {
      case ($color == 'blue' and $size == 'small'):
        echo "blue and small";
        break;
      case ($color == 'red' and $size == 'large'):
        echo "red and large";
        break;
      default:
        echo 'nothing';
        break;
    }

#2


27  

Using the new array syntax, this looks almost like what you want:

使用新的数组语法,这看起来几乎像你想要的:

switch ([$color, $size]) {
    case ['blue', 'small']:
        echo 'blue and small';
    break;

    case ['red', 'large'];
        echo 'red and large';
    break;
}

#3


16  

Doesn't work. You could hack around it with some string concatentation:

不起作用。你可以用一些字符串连接来解决它:

switch($color . $size) {
   case 'bluesmall': ...
   case 'redlarge': ...
}

but that gets ugly pretty quick.

但这很快就变丑了。

#4


7  

Found at http://www.siteduzero.com/forum/sujet/switch-a-plusieurs-variables-75351

发现于http://www.siteduzero.com/forum/sujet/switch-a-plusieurs-variables-75351

<?php
$var1 = "variable1";
$var2 = "variable2";
$tableau = array($var1, $var2);

switch ($tableau){
    case array("variable1", "variable2"):
        echo "Le tableau correspond !";
    break;

    case array(NULL, NULL):
        echo "Le tableau ne correspond pas.";
    break; 
}
?>

#5


1  

Your other option (though not pretty) is to nest the switch statements:

你的另一个选择(虽然不是很漂亮)是嵌套switch语句:

switch($color){
    case "blue":
        switch($size):
            case "small":
            //do something
            break;
    break;
}

#6


0  

var $var1 = "something";
var $var2 = "something_else";
switch($var1.$var2) {
case "somethingsomething_else":
    ...
    break;
case "something...":
    break;
case "......":
    break;
}