2016-11-10 80 views
0

我知道我可以使用以下内容来查看参数p是否存在于URL中。但是,如果(isset($ _ GET [ 'P'])){}PHP如果URL仅包含某个参数

我也知道,下面看到这个也会工作,如果还有AQ,T,R等参数

如果有任何参数设置

如果(计数($ _ GET)){}

但是我需要的是:

如果只有参数p存在 - >做一些事情...否则如果参数p存在和其他参数存在,否则做某事......否则如果参数!= p,或者没有参数存在 - 做别的事

任何提示将不胜感激

回答

1
if(isset($_GET['p']) && count($_GET['p']) == 1){ 
//do something 
} else if (isset($_GET['p'])){ 
//do something 
} else { 
//do something else 
} 
0

这显然是你在找什么:

<?php 
// ... 
if ((count($_GET) === 1) && isset($_GET['p'])) { 
    // GET argument 'p' exists and is the only one 
} elseif (isset($_GET['p']) { 
    // GET argument'p' exists 
} else{ 
    // GET argument'p' does _not_ exist 
} 
0
$wantedKeys = ['p']; 

if (array_diff_key($_GET, array_flip($wantedKeys))) { 
    echo 'keys other than ', join(', ', $wantedKeys), ' are in $_GET'; 
} 

这可扩展到任意数量的通缉键。
与组合“的需要的所有让利”:

if (
    count(array_intersect_key($_GET, array_flip($wantedKeys))) == count($wantedKeys) 
    && !array_diff_key($_GET, array_flip($wantedKeys)) 
) { 
    echo 'only the keys ', join(', ', $wantedKeys), ' are in $_GET'; 
} 
0

这里的嵌套if秒的替代解决方案(可能有更好的表现):

<?php 

if (isset($_GET['p'])) { # If parameter p exists 
    if (count($_GET) == 1) { # If only p exists 
     // Do something 
    } else { # If other parameters exist 
     // Do something else 
    } 
} else { # If p doesn't exist 
    // Do yet something else 
}