douguan8940 2014-06-21 16:55
浏览 53
已采纳

优化代码php动态包含[关闭]

I have the following code I found on the internet, I'm not very good with php but I would have a way to optimize the following code:

<?php

    $pages = array(
        "home" => array(
            "title"       => "Title name page",
            "display"     => "home.php",
            "description" => "Description name page",
        ),
        "order" => array(
            "title"       => "Title name page",
            "display"     => "order.php",
            "description" => "Description name page",
        ),
        "contacto" => array(
            "title"       => "Title name page",
            "display"     => "contacto.php",
            "description" => "Description name page",
        ),
        404   => array(
            "title"       => "Page Not Found",
            "display"     => "404.php",
            "description" => "The page you requested was not found!",
        )
    );

    require('config.php');

    if(isset($_GET["page"])) {
        if (isset($pages[$_GET["page"]])) {
            $page           = $_GET["page"];
            $title          = $pages[$page]["title"];
            $display        = $pages[$page]["display"];
            $description    = $pages[$page]["description"];
            include('header.php');
            include($display);
            include('footer.php');

        } else {
            $page           = 404;
            $display        = $pages[$page]["display"];
            $title          = $pages[$page]["title"];
            $description    = $pages[$page]["description"];
            include('header.php');
            include($display);
            include('footer.php');
        }
    } else {
        $page = "home";
        $display        = $pages[$page]["display"];
        $title          = $pages[$page]["title"];
        $description    = $pages[$page]["description"];
        include('header.php');
        include($display);
        include('footer.php');
    }

?> 

You believe that there is any way to optimize and includes many not use header / footer?

  • 写回答

1条回答 默认 最新

  • dstxpei5823 2014-06-21 17:04
    关注

    At least simplify all those if/else to eliminate the duplication

    if(isset($_GET["page"])) {
        if (isset($pages[$_GET["page"]])) {
            $page = $_GET["page"];
        } else {
            $page = 404;
       }
    } else {
        $page = "home";
    }
    
    $title       = $pages[$page]["title"];
    $display     = $pages[$page]["display"];
    $description = $pages[$page]["description"];
    include('header.php');
    include($display);
    include('footer.php');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 个人网站被恶意大量访问,怎么办
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制