$11.5

PHP Switch Statements with 2 Variables at a time

Ask a question+
3

I wondering if we can somehow have a switch statement in PHP that can use 2 variables at a time. Like:

switch($var1, $var2){
case($var1 == 'Value1' && $var2=='Value2'):
    echo "Yay! You did it!";
break;

}

Tried it but didn’t work.

Is there another way to do it?

add comment

1 Answer

5

You can use switch with true argument

switch(true){
    case($var1 == 'Value1' && $var2=='Value2'):
        echo "Yay! You did it!";
        break;
}

or check as array

switch ([$var1, $var2]) {
    case ['Value1', 'Value2']:
        echo 'Yay! You did it!';
        break;
}
add comment

Your Answer