另一个 switch 语句中的 PHP switch 语句 [英] PHP switch statement inside another switch statement

查看:45
本文介绍了另一个 switch 语句中的 PHP switch 语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到过这种情况,我必须检查两个 GET 变量.检查语句内的一个 switch 语句中的第一个变量后,必须在第一个 case 循环内的第二个 switch 语句中检查第二个变量.

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'];
    }
?>

这个代码示例运行良好,但是当我在我的服务器上实际使用这个过程时,即使它具有匹配的 case 值,控件也会在父 switch 语句中被跳过到默认值.

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.

推荐答案

根据 PHP 文档示例,如果包含嵌套开关的 case 块用大括号括起来是可能的.

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

遵循提到的示例.

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

这篇关于另一个 switch 语句中的 PHP switch 语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆