开发者

Exclude multiple values in if clause

开发者 https://www.devze.com 2023-03-09 21:40 出处:网络
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:)

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

精彩评论

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