简体   繁体   中英

I can't figure out php syntax

I have taxonomies that i need called here,

if ( $the_tax->name == 'day' , 'drink_type') {
        $tax_output = '';
    }
    return $tax_output;
}

The above code is written wrong, The taxonomy of day works, i can not figure out how to ref a second taxonomy of drink_type. do i use a comma? or || or what () if anyone knows php syntax and can help THANKS!

You either need logical OR (which can be written as || (common syntax) or just OR (which is PHP specific so I rather do not recommend using) - see logical operators doc for more details. So your code should look like this:

if( ($the_tax->name == 'day') || ($the_tax->name == 'drink_type') ) {
   // at least one condition met
}

or use in_array() if you going to have more allowed names:

$names = ['day','drink_type'];
if( in_array($the_tax->name, $names) {
  // allowed name...
}

Separate the conditions with || (logical OR)

if ( $the_tax->name == 'day' ||  $the_tax->name == 'drink_type') {
        $tax_output = '';
}
return $tax_output;
}

Given what you said, the correct syntax would be :

if ( $the_tax->name == 'day' || $the_tax->name == 'drink_type') { 
// Note that you can't use id ($var == 'X' || 'Y')

The correct syntax uses two pipes in between your two conditions of the if statement. Like this,

$tax_output;

if ( $the_tax->name == 'day' || $the_tax->name == 'drink_type') {
    $tax_output = '';
}
return $tax_output;

I left out your ending } because as is it will throw a syntax error. If that ending bracket is supposed to be there then you should add the part of the code that contains the opening bracket as well.

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