tags:

views:

75

answers:

2

Is there a way to include multiple cases inside a switch method in php?

A: 

Yes it is possible

Here is an example to start with

<?
      $i = 1;
      $j = 10;

      switch($i) {
            case "2":
                   echo "The value is 2";
                   break;
            case ($i==1 && $j==10):
                   echo "Your exceptional Switch case is triggered";
                   break;
            default:
                   echo "Invalid";
                   break;
     }
?>
Starx
Mark Baker
Mark Baker
+1  A: 

What's wrong with simply nesting switches?

$i = 1;
$j = 10;

switch($i) {
    case 2:
        echo "The value is 2";
        break;
    case 1:
        switch($j) {
            case 10:
                echo "Exception Case";
                break;
            default:
                echo "The value is 1";
                break;
        }
        break;
    default:
        echo "Invalid";
        break;
}
Mark Baker
its boring, try one of starx, its quicker
Starx
Read my comments about starx example.... it doesn't actually work correctly
Mark Baker