6

is there a way to the php's SWITCH but with 2 values? Here's what I'm looking for

switch(a, b){
    case 1,2: some code... ; break;
    case 3,4: some code... ; break;
    case 3,6: some code... ; break;
    case 5,2: some code... ; break;
    case 1,3: some code... ; break;
    case 8,5: some code... ; break;
}

I know this won't work, so how would i do something along these lines?

2 Answers 2

12

You could use an array with 2 elements since == comparison checks the array values:

$a = 3;
$b = 6;

switch([$a, $b]){
    case [1, 2]: echo '1'; break;
    case [3, 4]: echo '2'; break;
    case [3, 6]: echo '3'; break;
    case [5, 2]: echo '4'; break;
    case [1, 3]: echo '5'; break;
    case [8, 5]: echo '6'; break;
}

Outputs 3.

Sign up to request clarification or add additional context in comments.

Comments

2

You could use some string instead if it isn't heavy processing:

$variable= "1,2";

switch($variable){
    case "1,2": some code... ; break;
    case "3,4": some code... ; break;
    case "3,6": some code... ; break;
    case "5,2": some code... ; break;
    case "1,3": some code... ; break;
    case "8,5": some code... ; break;
}

2 Comments

@wes: Yes I find PaulPRO's answer more accurate. Mine was just another possibility I shared :)
This wasn't the best solution, however it would work. So upvote from me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.