Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Switch case with three parameters?

Tags:

php

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

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

If not, should I just use if-else or is there a better solution?

like image 892
Johan Avatar asked Aug 25 '09 14:08

Johan


People also ask

Can we pass multiple parameters in switch case?

Yes as other state. You can have only one expression but that expression can have multiple variables. Although, for readability, we recommend put a simple expression in the switch.

Can switch case take parameters?

You can only pass one argument to the switch statement.


2 Answers

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;
}
like image 88
soulmerge Avatar answered Sep 23 '22 18:09

soulmerge


I would just use the if/else

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

or

if($var1 && !($var2 && $var3)) {
    echo "Hello";
}
like image 37
JasonDavis Avatar answered Sep 23 '22 18:09

JasonDavis