2015-12-18 96 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