douran7929 2011-02-08 02:23 采纳率: 100%
浏览 10
已采纳

PHP Case开关(效率)

The code below ensures that when a user accesses control panel, they are ran through a quick verification process to validate what their entities are. For instance, if a user is level 1 they are only given access to video feed which means nothing else is available to them.

When I look at the code though, I can see video feed being called when case 1 and 3 are called. I would possibly enjoy an alternative to make the code more efficient.

I was told a possible array could make things a little easier but then again this is faster.

    switch ($_SESSION['permission']) {
        case 1: // Level 1: Video Feed
            include ("include/panels/videofeed.index.php");
            break;
        case 2: // Level 2: Announcements / Courses / Teachers
            include ("include/panels/announcements.index.php");
            include ("include/panels/courses.index.php");
            include ("include/panels/teachers.index.php");
            break;
        case 3: // Level 3: Announcements / Video Feed / Courses / Teachers / Accounts / Logs
            include ("include/panels/announcements.index.php");
            include ("include/panels/videofeed.index.php"); 
            include ("include/panels/courses.index.php");
            include ("include/panels/teachers.index.php");
            include ("include/panels/accounts.index.php");
            include ("include/panels/log.index.php");       
            break;
        case 4: // Level 4: Teachers
            include ("include/panels/teachers.index.php");          
    }
  • 写回答

2条回答 默认 最新

  • doukanwa6872 2011-02-08 02:41
    关注

    Frist you may run better if you use require_once if its possible. The second point is to shorten the url it seems that its every include the same.

    Maybe try to use it in a function for example:

    $permission_id = $_SESSION['permission']
    
    function requireModule($moduleName, $path = 'include/panels/') {
        $moduleName .= '.index.php';
        require_once($path . $moduleName);
    }
    
    // if you want you can add an arry for it:
    
    $permissionModules = array(
        array('videofeed'),
        array('announcements', 'courses', 'teachers'),
        array('announcements', 'courses', 'teachers', 'accounts', 'log', 'videofeed'),
        array('teachers')
    );
    
    // now we can put it in a more effectiv way
    
    if(array_key_exists($permission_id, $permissionModules)) {
        foreach($permissionModules[$permission_id] as $includes) {
            requireModule($includes);
        }
    }
    

    Wrong ? Correct me!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料