php if语句对照列表,需要帮助简化

php if语句对照列表,需要帮助简化,php,if-statement,Php,If Statement,因此,我正在尝试为一些基本站点构建一个小型轻量级框架,我正在为我的托管客户构建这些站点,并尝试调用index.php 我已经弄明白了,但觉得必须有更好的方法来编写以下if语句: <?php if('home'==$currentpage) { include('shared/footer.htm'); } elseif('testing'==$currentpage) {

因此,我正在尝试为一些基本站点构建一个小型轻量级框架,我正在为我的托管客户构建这些站点,并尝试调用index.php

我已经弄明白了,但觉得必须有更好的方法来编写以下if语句:

<?php
        if('home'==$currentpage)
        {
        include('shared/footer.htm');
        }   
        elseif('testing'==$currentpage)
        {
        include('shared/footer.htm');
        }
        elseif('training'==$currentpage)
        {
        include('shared/footer.htm');
        }
        elseif('contact'==$currentpage)
        {
        include('shared/footer.htm');
        }
        elseif('pricing'==$currentpage)
        {
        include('shared/footer.htm');
        }
    ?>
其中一个会在联系人页面上显示footer.htm,但其他的都不会显示,如果我切换它,那么它会显示任何项目最后的结果,我还尝试了foreach语句,它会破坏页面,所以我放弃了它,并想请求lil帮助


提前感谢。

您可以使用简单数组列表:

$arr = array('home', 'testing', 'training', 'contact','pricing');
if (in_array($currentpage,$arr))
{
   include('shared/footer.htm');
}
$arrPage = array(
 'home' => 'shared/footer.htm',
 'testing' => 'shared/footer.htm',
 'training' => 'shared/footer.htm',
 'contact' => 'shared/footer.htm',
 'pricing' => 'shared/footer.htm',
);
if( array_key_exists( $arrPage, $currentPage ))
    include($arrPage[$currentpage]);

您可以使用简单数组列表:

$arrPage = array(
 'home' => 'shared/footer.htm',
 'testing' => 'shared/footer.htm',
 'training' => 'shared/footer.htm',
 'contact' => 'shared/footer.htm',
 'pricing' => 'shared/footer.htm',
);
if( array_key_exists( $arrPage, $currentPage ))
    include($arrPage[$currentpage]);

你可以有一个地图,然后用它来调用正确的页面。如果文件没有不同的路径,可以删除路径。我想那是个打字错误

$pages = array(
   'home' => 'shared/footer.htm',
   'testing' => 'shared/footer.htm',
   'training' => 'shared/footer.htm'
); //and so forth

if (isset($pages[$currentpage])) {
    include($pages[$currentpage]);
} else {
   //show default page
}

你可以有一个地图,然后用它来调用正确的页面。如果文件没有不同的路径,可以删除路径。我想那是个打字错误

$pages = array(
   'home' => 'shared/footer.htm',
   'testing' => 'shared/footer.htm',
   'training' => 'shared/footer.htm'
); //and so forth

if (isset($pages[$currentpage])) {
    include($pages[$currentpage]);
} else {
   //show default page
}