另一个 switch 语句中的 PHP switch 语句
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9353240/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP switch statement inside another switch statement
提问by pahnin
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.
我有这种情况,我必须检查两个 GET 变量。在检查语句内的一个 switch 语句中的第一个变量后,必须在第一个 case 循环内的第二个 switch 语句中检查第二个变量。
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.
它在这个代码示例中工作正常,但是当我在我的服务器上实际使用这个过程时,即使它具有匹配的 case 值,控件也会在父 switch 语句中被跳过到默认值。
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.
我知道你想看代码,但我不能把它贴在这里。至少你可以指导我调试更多。
回答by Nereare
According to the PHP Documentation Examples, it is possible if the case block which contains the nested switch is enclose with braces.
根据PHP 文档示例,如果包含嵌套开关的 case 块用大括号括起来是可能的。
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;
}
}
}
}
}
}
?>
回答by Alexander Jank
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 语句,因为 switch 语句定义如下:
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.
[statement]
可以替换为简单的命令,例如echo "test";
,一系列命令echo "test"; echo "test";
或其他 switch 语句。添加大括号不会改变任何东西,因为它们只是用于对一系列命令进行分组 - 这样就可以将多个命令用于then
像if([condition]) [then]
. 上面的答案出人意料地有 18 票赞成错误地建议您添加大括号,但并没有改变任何东西。
To conclude, nested switch statements are possible and the way you used them in your code sample is correct. The error is somewhere else.
总而言之,嵌套 switch 语句是可能的,并且您在代码示例中使用它们的方式是正确的。错误在其他地方。