如何解决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\',\'training\' => \'shared/footer.htm\'
); //and so forth
if (isset($pages[$currentpage])) {
include($pages[$currentpage]);
} else {
//show default page
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。