PHP switch statement inside another switch statement

前端 未结 2 1824
忘了有多久
忘了有多久 2021-02-19 05:05

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 i

2条回答
  •  没有蜡笔的小新
    2021-02-19 05:36

    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.

提交回复
热议问题