7

While researching for better ways to make use of a switch statement, I found this stackoverflow example. I wanted to do something similar, but with a twist:

switch($status)
{
 case "a":
 case "b":
  echo "start execute code for case a and b";
 case "a":
  echo "continue to execute code for case a only";
 case "b":
  echo "continue to execute code for case b only";
 case "a":
 case "b":
  echo "complete code execution for case a and b";
 break;
 case "c":
  echo "execute code for case c";
 break;
 case "d":
  echo "execute code for case d";
 break;
 case "e":
  echo "execute code for case e";
 break;
 case "f":
  echo "execute code for case f";
 break;
 default:
  echo "execute code for default case";
}

Yes, the above obviously doesn't work out as planned because case "a" will fall-through through until it hits a break. I just want to know whether there is a way to do this elegantly without repeating too much code.

Community
  • 1
  • 1
Question Overflow
  • 10,925
  • 18
  • 72
  • 110

5 Answers5

12

Here is what I think would be an elegant solution:

switch($status)
{
 case "a":
 case "b":
  echo "start execute code for case a and b";
  if($status == "a") echo "continue to execute code for case a only";
  if($status == "b") echo "continue to execute code for case b only";
  echo "complete code execution for case a and b";
 break;
 case "c":
  echo "execute code for case c";
 break;
 case "d":
  echo "execute code for case d";
 break;
 case "e":
  echo "execute code for case e";
 break;
 case "f":
  echo "execute code for case f";
 break;
 default:
  echo "execute code for default case";
}

I am not trying to invent anything new here. Just trying to learn from the experiences of everyone here. Thanks to all who provided me with answers.

Question Overflow
  • 10,925
  • 18
  • 72
  • 110
  • Don't know why this isn't ranked higher. This is the simplest solution and also the most legible for future maintainers of the code. – Jazz Apr 06 '12 at 16:27
6

Once a case is matched, PHP will ignore any further case statements and execute ALL code until either the switch is closed (}) or a break is encountered. break will also terminate the switch, so what you want is not possible.

Marc B
  • 356,200
  • 43
  • 426
  • 500
1

This is not correct switch statement usage.

You should replace your switch with a series of if statements.

if($status == a || $status == b ) {
   echo "start execute code for case a and b";
   if(status == a) {
      echo "continue to execute code for case a only";
   }
   else {
      echo "continue to execute code for case b only";   
   }
   echo "complete code execution for case a and b";
}
else if ($status == c) {
   echo "execute code for case c";  
}
...
...
else {
   echo "execute code for default case";
}
Colin D
  • 5,641
  • 1
  • 23
  • 35
1

There is no how in the would $status can be = a AND b except if its a array i kno what you want to do and this is my prove of concept

function runSwitch($status) {

    if (in_array ( "a", $status ) && in_array ( "b", $status )) {
        echo "start execute code for case a and b" . PHP_EOL;
    }

    if (in_array ( "a", $status )) {
        echo "continue to execute code for case a only" . PHP_EOL;
    }

    if (in_array ( "b", $status )) {
        echo "continue to execute code for case b only" . PHP_EOL;
    }

    if (in_array ( "c", $status )) {
        echo "execute code for case c" . PHP_EOL;
    }

    if (in_array ( "d", $status )) {
        echo "execute code for case d" . PHP_EOL;
    }

    if (in_array ( "e", $status )) {
        echo "execute code for case e" . PHP_EOL;
    }

    if (in_array ( "f", $status )) {
        echo "execute code for case f" . PHP_EOL;
    }

    if (in_array ( "c", $status ) && in_array ( "f", $status )) {
        echo "continue to execute code for case c AND f only" . PHP_EOL;
    }

}

Example 1

$status = array (
        "a" 
);

runSwitch($status);

Output

continue to execute code for case a only

Example 2

$status = array (
        "a" , "b"
);


runSwitch($status);

Output

start execute code for case a and b
continue to execute code for case a only
continue to execute code for case b only

I hope this helps

Thanks

Baba
  • 94,024
  • 28
  • 166
  • 217
  • thanks for your help, but $status is a single character string, not an array. I think I know how to do it. I am just amuse at the kind of response I am getting by others. – Question Overflow Apr 06 '12 at 15:57
0

As Marc B said what you're trying (at least with a switch) can't be done. Fortunately a good way to remove duplicate code is to define methods, that way code common to both a and b can be called when required.

Jim
  • 22,354
  • 6
  • 52
  • 80