等於多個字符串我發現了類似對這個問題的東西,但它是不相關的字符串。我只是在尋找寫下面代碼的最佳方式,我認爲必須有一些我不知道的簡短版本。如果變量在導航列表
<ul id="top-nav">
<li <?php if($filename == "index.php"){echo ("class=\"current\"");} ?>><a href="index.php" title="Home">Home</a></li>
<li <?php if($filename == "cause.php"){echo ("class=\"current\"");} ?>><a href="cause.php" title="Cause">Cause</a></li>
<li><a href="order.php" title="Order">Order</a></li>
<li class="drop-down<?php if($filename == "grand-prize-one.php" | $filename == "grand-prize-two.php"){echo (" current");} ?>"><a href="grand-prize-one.php" title="Grand Prizes">Grand Prizes</a>
<ul>
<li><a href="grand-prize-one.php" title="Grand Prize One">Grand Prize One</a></li>
<li><a href="grand-prize-two.php" title="Grand Prize Two">Grand Prize Two</a></li>
</ul>
</li>
<li <?php if($filename == "early-bird.php"){echo ("class=\"current\"");} ?>><a href="early-bird.php" title="Early Bird">Early Bird</a></li>
<li class="drop-down<?php if($filename == "vehicles.php" || $filename == "vacations.php" || $filename == "other-prizes.php"){echo (" current");} ?>"><a href="vehicles.php" title="Electronics & more">Other Prizes</a>
<ul>
<li><a href="vehicles.php" title="Vehicles">Vehicles</a></li>
<li><a href="vacations.php" title="Vacations">Vacations</a></li>
<li><a href="other-prizes.php" title="Electronics & more">Electronics & more</a></li>
</ul>
</li>
<li <?php if($filename == "winners.php"){echo ("class=\"current\"");} ?>><a href="winners.php" title="Winners">Winners</a></li>
</ul>
是否有寫$filename == "vehicles.php" || $filename == "vacations.php" || $filename == "other-prizes.php"
較短的方法嗎?我懷疑我會需要它來適應更多的5-10個可能的字符串。
謝謝
SOLUTION
對於那些誰發現了這個問題,我最後做的:
隨着我的主要PHP
<?php
// My other code
$grandPrizes = array("grand-prize-one.php","grand-prize-two.php");
$otherPrizes = array("vehicles.php","vacations.php","other-prizes.php");
?>
我的直列PHP中的li元素
<?php if (in_array($filename, $grandPrizes)) {echo " current";} ?>
請使用||而不是|進行或比較。 – eisberg 2011-03-23 15:15:18
||是一個邏輯運算符,|是一個按位運算符 – 2011-03-23 15:21:22
哦,哇。謝謝。我不知道。我不知道我從哪裏得到這些信息,但它對我來說總是有效的。只是好奇,爲什麼它工作?如果任一$ a或$ b爲真 – 2011-03-23 15:22:21