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


Compare a variable against more than one (an array of) variables, with the same outcome, in PHP

我有一个简单的导航,它使用php来知道你在哪个"当前页面",允许我的css来指示这个

我的PHP代码:
<?php echo "'n"; if ($currentPage == 'about.php') { ?>

我想做的是让这个按钮在它的子页面中作为当前页面活动?是否有在上述代码中有多个页面的任何方式?

我试过了,但是行不通

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

您可以使用in_array:

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

它将遍历一个数组并将值($currentPage)与数组中的每个值进行比较

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

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

(不是回答只是一个更新)

这是我现在所拥有的:(只有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 } ?>

这就是为什么它选择原来的页面而不是我的新页面吗?