0

如果页面与我声明的页面相同,我已经编写了一个函数来使菜单滚动。

函数看起来像这样

 function menu_current()
 {
    $current = basename($_SERVER['REQUEST_URI']);
    if ($current === "index?p=config" || "index?p=maintenance")
        echo "class=\"nav-top-item suballowed current\" ";
    else
        echo "class=\"nav-top-item suballowed\" ";
 }

如果我只声明 1 页,它会完美运行

if ($current === "index?p=config")

但不多。如何解决该解决方案?有没有一种方法可以将标签declare之间的所有网站合二为一,而不是像我一样编写它们?||variable

4

4 回答 4

4

替换这个

if ($current === "index?p=config" || "index?p=maintenance")

 if ($current === "index?p=config" || $current ===  "index?p=maintenance")

否则 PHP 不知道应该等于什么index?p=maintenance

于 2013-07-09T18:18:25.047 回答
1

另一种选择是使用带有默认值的 switch 语句。

function menu_current()
{
    $current = basename($_SERVER['REQUEST_URI']);
    switch($current) {
        case "index?p=config":
        case "index?p=maintenance":
            echo "class=\"nav-top-item suballowed current\" ";
            break;
        default:
            echo "class=\"nav-top-item suballowed\" ";
    }
}
于 2013-07-09T18:41:28.120 回答
1

如果您每次都设置相等检查的两侧,则可以使用您的方法:

if ($current === "index?p=config" || $current === "index?p=maintenance") { ...

也许是一个更“可读”的解决方案:

if (in_array($current, array( 'index?p=config', 'index?p=maintenance' )) { ...
于 2013-07-09T18:18:56.307 回答
0

这是正确的代码

  function menu_current()
     {
        $current = basename($_SERVER['REQUEST_URI']);

        if ($current == "index?p=config" || $current == "index?p=maintenance")
            echo "class=\"nav-top-item suballowed current\" ";
        else
            echo "class=\"nav-top-item suballowed\" ";
     }
于 2013-07-09T18:20:02.687 回答