简体   繁体   中英

How can i add exception to php if statement

I have this if statement but how can I add more conditions to it?:

if($row['Type']==$rtype){
}

I would like to have two conditions so when $row['Type']=A and $rtype=B and vise versa all others should be match exact but in case when Type A and B comes together I want to allow the statement display result.

I tried by adding:

($row['Type']=='A' && $rtype=='B') || ($row['Type']=='B' && $rtype=='A')

as:

if(...&& (($row['Type']==$rtype) || ($row['Type']=='A' && $rtype=='B') || ($row['Type']=='A' && $rtype=='B'))) {
 }

What is the best way to approach this?

From your comment above, it sounds like you may need more clearly define when to "show" and when to "not show". Typically, if there are only two scenarios — either show or don't show — then you wouldn't need two if conditions.

However, with what you describe above, this should work:

if (($rtype == 'A' && $row['Type'] == 'B') || ($rtype == 'B' && $row['Type'] == 'A')) {
    return true;
}

if (($rtype == 'D' && $row['Type'] == 'D') || ($rtype == 'E' && $row['Type'] == 'E') || ($rtype == 'F' && $row['Type'] == 'F') {
    return false;
}

Note that each group is contained within its own ( and ) . That does the AND you're looking for. Outside of each group, the || will do the OR .

If you know that you don't want to "show" if the first if doesn't evaluate to true , then you can simply do:

if (($rtype == 'A' && $row['Type'] == 'B') || ($rtype == 'B' && $row['Type'] == 'A')) {
    return true;
}

return false;

I'm not sure what "show" and "don't show" mean so I'm just assuming a true/false will do for you.

Also check out the Operator Precedence link that abhishek posted above. That might give you some more general knowledge about how to group your logical expressions.

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