Home » Php » php – Making use of a fall-through switch

php – Making use of a fall-through switch

Posted by: admin July 12, 2020 Leave a comment

Questions:

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.

How to&Answers:

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.

Answer:

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.

Answer:

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";
}

Answer:

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

Answer:

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.