2015-12-18 97 views
-1

我在代码中执行此if语句,以便在检查中的页面上添加横幅<div>。我怎样才能更好地整理这些代码?缩短if语句

$currentpage = $_SERVER['REQUEST_URI']; 
    if($currentpage=="/" || $currentpage=="/en" || $currentpage=="/pl" || $currentpage=="/cz" || $currentpage=="/index.php" || $currentpage=="/index.php/" || $currentpage=="" || 
     $currentpage=="/en/index.php" || $currentpage=="/en/index.php/" || 
     $currentpage=="/pl/index.php" || $currentpage=="/pl/index.php/" || 
     $currentpage=="/cz/index.php" || $currentpage=="/cz/index.php/" || 
     $currentpage=="/flyeuro/en/index.php/" || $currentpage=="/flyeuro/en/index.php" || 
     $currentpage=="/flyeuro/pl/index.php/" || $currentpage=="/flyeuro/pl/index.php" || 
     $currentpage=="/flyeuro/cz/index.php/" || $currentpage=="/flyeuro/cz/index.php") 
    { 
+0

为什么减。我所要问的只是如何整理这些混乱的代码,这有什么问题? – zzwyb89

+2

制作白名单并循环播放/或使用in_array –

回答

2

你可以这样做:

<?php 
    // add your all URLs to an array 
    $urls = array("/en/index.php", "/en/index.php/", "/", "/en"); 

    if(in_array($currentpage, $urls)) { 
     echo "It exists"; 
    } else { 
     echo "It does not exist"; 
    } 
?> 
+0

辉煌,谢谢! – zzwyb89