PHP switch statement inside another switch statement

32,899

Solution 1

According to the PHP Documentation Examples, it is possible if the case block which contains the nested switch is enclose with braces.

Follows the mentioned example.

<?php 
    switch ($argc) { 
        case 'home': { 
             print('This is $argc, home case.'); 
            break; 
        } 
        case 'subsection': { 
                switch ($argd) { 
                     case 'links': { 
                            switch($arge) { 
                                case 'display': { 
                                print('This is $arge, subsection,links,display case.'); 
                                break; 
                                } 
                           } 
                    } 
                } 
        } 
    } 
?>

Solution 2

I can't see any error in your code. I tried it on my machine and it works fine. As it also works fine for you, the error most be somewhere else.

There's no reason why nested switch statements shouldn't be possible, as switch statements are defined as follows:

switch($variable) {
  case "case1":
    [statement]
    break;
}

[statement] can be replaced by a simple command, such as echo "test";, a series of commands echo "test"; echo "test"; or another switch statement. Adding braces doesn't change anything, as these merely serve to group a series of commands - so that more than one command can be used for the then part in conditionals like if([condition]) [then]. The above answer, which surprisingly has 18 upvotes for wrongly advising you to add braces, doesn't change anything.

To conclude, nested switch statements are possible and the way you used them in your code sample is correct. The error is somewhere else.

Share:
32,899
pahnin
Author by

pahnin

Programmer (c,cpp,javascrip,php,python) recently fell in love with ruby. I hope I'm gonna end up being rubyist.

Updated on July 15, 2020

Comments

  • pahnin
    pahnin almost 4 years

    I have this situation where I have to check two GET variables. After checking the first one in one switch statement inside the statement, the second variable has to be checked in the second switch statement inside the first one case loop.

    I can't post the exact code here, but here is an example:

    <?php
        error_reporting(E_ALL);
        ini_set('display_errors', '1');
    
        switch($_GET['parent']){
            case 'child1':
                if(!isset($_GET['child'])){
                    echo "Only parent";
                }
                else{
                    switch($_GET['child']){
                        case 'test':
                            echo 'test';
                            break;
                    }
                }
                break;
    
            case 'child2':
                echo 'child2';
                break;
    
            default:
                echo $_GET['parent'];
        }
    ?>
    

    It's working fine with this code example, but when I actually use this procedure on my server, the control get skipped to default at parent switch statement even though it has a matching case value.

    No error is reported, and I couldn't debug more than to this level.

    I know you want to see the code, but I cannot post it here. At least you can guide me debug more.

    • Michael Berkowski
      Michael Berkowski about 12 years
      I don't see a default case for the parent switch. What exactly are you getting for output?
    • pahnin
      pahnin about 12 years
      well I havent added default here but I have added in the real problem
    • Michael Berkowski
      Michael Berkowski about 12 years
      Please post the URL as you are entering it, which is failing on this script. It should parse example.com?parent=child1 just fine.
    • jk.
      jk. about 12 years
      I can't seem to duplicate the issue: codepad.org/uoYT3WXS Try editing the codepad example to duplicate your problem.
  • gillespieza
    gillespieza almost 4 years
    Do you know why this example is downvoted in the documentation?
  • Patrick
    Patrick almost 2 years
    A quick test suggest that you would need to break the main switch statement aswell as break the nested switch statement