我在互联网上找到了以下代码,我对PHP不太满意,但我想方法优化以下代码:
<?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');
}
?>
您认为有任何方法可以优化并包含许多不使用页眉/页脚的内容吗?
答案 0 :(得分:1)
至少简化所有if / else以消除重复
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');