views:

132

answers:

8

How can I simply the below if statements?

if ( isset(var1) & isset(var2) ) {

    if ( (var1 != something1) || (var2 != something2) ) {

        // ... code ...
    }

}

Seems like this could be condensed to only one IF statement but am not certain if I'd use an AND or OR

+1  A: 
if (isset(var1) && ((var1 != something1) || (var1 != something2)))
    // ... code ...
}

You would use an and because you can only get to the // ... code ... part if both if-statements are true.

Matt Ball
Sorry bears, my original code was incorrect.
JGreig
+1  A: 

You can do:

if(isset(var1) && isset(var2) && ( (var1 != something1) || (var1 != something2) ) ){
    //..code
}   

As a general example:

if( cond1 && cond2 ) {
 if( cond3 || cond4) {
   // ...code..
 }
}

The code will be executed only when both cond1 and cond2 are true and either of cond3 or cond3 is true.

codaddict
Sorry unicornaddict, my original code was incorrect.
JGreig
codaddict
Perhaps `isset(var2)` does some processing that needs to take place regardless of whether the first `isset()` returned true?
Lord Torgamus
+2  A: 
Boolean varsAreSets = isset(var1) & isset(var2); // or some other name that indicates what this is doing
Boolean someMeaningfulName = (var1 != something1) || (var2 != something2); // would suggest a meaningful name but don't know what this is accomplishing

if ( varsAreSets && someMeaningfulName ) { 
        // ... code ... 
} 

This makes the code very readable and helps you and whoever reads the code understand what these checks are actually doing.

froadie
I personally don't like this style and would rather have a useful comment or two around the original checks within the if block.
Joe
A: 
if ( isset(var1) && isset(var2) && ( (var1 != something1) || (var2 != something2) ) ) {

    // ... code ...
}
Devon_C_Miller
+1  A: 

It's a question of in what order your computer interprets boolean logic:

Take for example the following conditions:

A: False B: True

if you were to write if (A && B) what your computer actually does is think:

Is A true? No.

Well, A and B can't be true because A isn't true. Therefore this statement is false. [computer ignores the rest of the logic]

Because of this, when you evaluate the statement isset(var1) && ( (var1 != something1) || (var1 != something2) ) it first checks isset(var1) and if that's false, then it skips the rest of the condition, just like your double-if statement.

amphetamachine
+1 for explanation of processing logic
Slokun
that is language dependent no? As I remember VB6 would evaluate all conditions even if the first evaluation meant the condition was always false (or true). Might be wrong though
Sam Holder
It is language and settings dependent. Turbo Pascal and Delphi, for example, have had a compiler switch for ages to disable boolean short-circuit evaluation (aka always evaluate the entire expression). The default is short-circuit enabled.
dthorpe
@dthorpe - But who uses those languages?
amphetamachine
@amphetamachine: Quite a few folks, actually. I cited Turbo Pascal and Delphi because those are the tools I'm most familiar with (having worked on them at Borland), but short circuit eval is common across a lot of languages. See the chart on this wikipedia page: http://en.wikipedia.org/wiki/Short-circuit_evaluation
dthorpe
A: 
JohnB
A: 

Another option:

if (isset(var1) && isset(var2)
&& !(var1 == something1 && var2 == something2)) {
   ...
Nick D
A: 

I think most of the examples above that have 1 IF may spit out an error if var1 or var2 is NOT set

(isset($var1) && isset($var2)) ? ($var1!='something1' && $var2!='something2') ? $go=TRUE : $go=FALSE : $go = FALSE;

if ($go){
    echo 'hello';
}
Amien