php if-statement语句针对列表,需要帮助以简化。

| 因此,我试图为我为托管客户构建的一些基本站点构建一个小型的轻量级框架,并且尝试调用index.php中的各种include。 我已经弄清楚了,但是觉得必须有一种更好的方法来编写以下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\');
        }
    ?>
我得到以下工作来工作,它使用列表中的最后一项:
$arr = array(\'home\', \'testing\', \'training\', \'contact\');
        foreach ($arr as &$value);

        if ($value==$currentpage)
        {
        include(\'shared/footer.htm\');
        }
那将在联系人页面上显示footer.htm,但没有其他显示,如果我切换它,则显示最后一个项目结束了,我也尝试了一个foreach语句,它破坏了页面,所以我给了它想通了,我想寻求一点帮助。 提前致谢。     
已邀请:
        
$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]);
    
        您可以拥有一张地图,然后使用它来调用正确的页面。如果您的文件路径不同,则可以删除路径。我假设这是一个错字。
$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
}
    

要回复问题请先登录注册