开发者

Exclude multiple values in if clause

I have 30 sites and I need to echo something on 24 of them. How can I exclude the others? This code doe开发者_JS百科sn't work cause i think it's logic is faux:)

$currentsite = get_bloginfo('wpurl'); // Here i get the curent site.

If the current site matches any of the 6 below, the if clause should not run.

if ( $currentsite != 'site 1' || 

$currentsite != 'site1' || 

$currentsite != 'site2'|| 

$currentsite != 'site3' || 

$currentsite != 'site4' ||

$currentsite != 'site5' ) {

do something
}


You can put your url-s in an array of strings:

$linksArray = array();
$linksArray[] = 'site1';
$linksArray[] = 'site2';
$linksArray[] = 'site3';
$linksArray[] = 'site4';
$linksArray[] = 'site5';
$linksArray[] = 'site6';

and after that you can use the in_array() function like this:

if (!in_array($currentsite, $linksArray) {
   // echo your something
}

So it will echo your text if the current url is not in the array containing the excludable urls.


In your example above, you would need to replace the ||s with &&s:

if ($current_site != 'site1' && $current_site != 'site2' ...) 

A simpler approach would be to create an array of the sites you are excluding, and negate an in_array check:

$excluded_sites = array ('site1','site2','site3');

if (!in_array($current_site, $excluded_sites)) {
    do something...
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜