Mike Sav
Mike Sav

Reputation: 15291

Can a switch statement take two arguments?

is it posible for a PHP switch statement to take 2 arguements? For example:

switch (firstVal, secondVal){

    case firstVal == "YN" && secondVal == "NN":
    thisDesc = "this is the outcome: YN and NN";
    break;

    case firstVal == "YY" && secondVal == "NN":
    thisDesc = "this is the outcome: YY and NN";
    break;
}

Many thanks, I haven't done PHP in years!

Upvotes: 5

Views: 4398

Answers (5)

Steffen
Steffen

Reputation: 11

Old question, but i think this answer is not covered here:

You can pass multiple arguments to an array and pass that as the argument to the switch:

switch([$fooBah, $bool]) {
            case ['foo', true]:
                ...
                break;
            case ['foo', false]:
            case ['bah', true]:
                ...
                break;
            case ['bah', false]:
                ...
                break;
            ...
        }

Upvotes: 1

Philibert Gentil
Philibert Gentil

Reputation: 1

i have the answer ! You can do that by remove the "break" instruction. if you do

$i = 2;
switch ($i) {
    case 1:
        echo "a";
        break;
    case 2:
        echo 'b';
        break;
    case 2:
        echo 'c';
        break;
    case 3:
        echo 'd';
        break;
}

Your script will stop at the first break. So the output is b.

Now, if you remove the break instruction, the switch will continue to the end and verify all.

$i = 2;
switch ($i) {
    case 1:
        echo "a";
        break;
    case 2:
        echo 'b';
    case 2:
        echo 'c';
        break;
    case 3:
        echo 'd';
        break;
}

Output : cb So you can do 2 conditionnal instruction like this :

$i = 3;
switch ($i) {
    case 1:
        echo "a";
        break;
    case 2:
    case 3:
        echo 'd';
        break;
}

Output: d Your computer will read the case 2: and the result it false, but there is no break instruction, so the computer will continue and read case 3: >> True and the result is true : the computer execute your instruction. :)

If you didn't understand my explanation, i own you to read the switch php manual here.

Upvotes: -1

Mat
Mat

Reputation: 206727

No, but if your case is as simple as what you have, just concatenate the two inputs and test for the concatenated values:

switch ($firstval . $secondval) {
 case "YNNN": ...
 case "YYNN": ...
}

Upvotes: 3

KingCrunch
KingCrunch

Reputation: 131931

No, you can't. A switch-statement like

switch ($a) {
  case 1:
  break;
  case 2:
  break;
}

which is effectively the same as

if ($a == 1) {
} else if ($a == 2) {
}

You can use a slightly different construction

switch (true) {
  case $firstVal == "YN" && $secondVal == "NN":
  break;
  case $firstVal == "YY" && $secondVal == "NN":
  break;
}

which is equivalent to

if (true == ($firstVal == "YN" && $secondVal == "NN")) {
} else if (true == ($firstVal == "YY" && $secondVal == "NN")) {
}

In some cases its much more readable instead of infinite if-elseif-else-chains.

Upvotes: 6

Oscar Gomez
Oscar Gomez

Reputation: 18488

I am pretty sure you cannot do that, not only in PHP but any other programming language.

Upvotes: 0

Related Questions