简体   繁体   中英

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 doesn'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...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM