在PHP中将变量与多个变量(一组数组)变量进行比较,结果相同

时间:2013-02-26 14:48:15

标签: php navigation

我有一个简单的导航,它使用php来知道你正在使用哪个“当前页面”,允许我的CSS表明这一点。

我的PHP代码:

<?php echo "\n"; if ($currentPage == 'about.php') { ?>

我要做的是将此按钮作为其子页面中的当前页面激活? 有没有办法在上面的代码中有多个页面?

我尝试了这个,但它不起作用

<?php echo "\n"; if ($currentPage == 'about.php about2.php about3.php') { ?>

3 个答案:

答案 0 :(得分:3)

您可以使用in_array

<?php $pages = Array("about.php","about2.php","about3.php"); ?>
<?php echo "\n"; if (in_array($currentPage,$pages)) { ?>

这将基本上通过一个数组并将值($currentPage)与数组中的每一个进行比较。

答案 1 :(得分:1)

您可以使用in_array()功能:http://php.net/manual/en/function.in-array.php

if (in_array($currentPage, array('about.php', 'about2.php'))) {
// Do Something
}

答案 2 :(得分:0)

(不是答案只是更新)

这就是我现在所拥有的:(只有about.php激活开关)

    <?php $pages = Array("about.php","about2.php","about3.php"); ?>
        <?php echo "\n"; if (in_array($currentPage,$pages)) { ?>
            <li class="button on">
            <img src="style/images/clear.png" width="100px" height="100px" alt="clear_image" />
            About
            </li>
            <?php } else { ?>
            <li class="button off">
                <a class="nav" href="about.php">
                    <img src="style/images/clear.png" width="100px" height="100px" alt="clear_image" />
                    About
                </a>
            </li>
        <?php } ?>

如果我将页面更改为home.php,contact.php(最初在导航中),他们都会同时将它们全部打开? 所以我不确定我需要做什么才能包含我的新页面?

我这是我的其他按钮之一:

     <?php echo "\n"; if ($currentPage == 'contact.php') { ?>
            <li class="button on">
                <img src="style/images/clear.png" width="100px" height="100px" alt="clear_image" />
                Contact
            </li>
            <?php } else { ?>
            <li class="button off">
                <a class="nav" href="contact.php">
                    <img src="style/images/clear.png" width="100px" height="100px" alt="clear_image" />
                    Contact
                </a>
            </li>
        <?php } ?>

这是为什么它会拿起原始页面而不是我的新页面?