Matheus Oliveira
Matheus Oliveira

Reputation: 627

Do we always test the two members of "AND" and "OR" in "IF" statements?

My question is simple, if I have the code below:

$var = 'foo';
$var2 = 'bar';

If ($var == 'bar' && $var2 == 'foo'){
    [.. code not executed for the first condition..]
}

Or the code below

If ($ var == 'foo' || $var2 == 'bar'){
   [..code executed for the first condition...
}

Both IF statements have the action decided in the first condition.

The second test is executed ?

UPDATE

I am asking this, because I was thinking about accessing a property inside a object that may or may not exist.

So I would try something like that:

if ($this->object->getVar() != null && $this->object->GetVar() == 'foo'){
    [..does something if var exist and if it is 'foo'. ]
}

But maybe this isn't a good practice, I imagine.

Upvotes: 0

Views: 51

Answers (3)

Thorsten Dittmar
Thorsten Dittmar

Reputation: 56707

There are several aspects to keep in keep apart here:

Boolean operations

  1. You can generally omit checking all remaining conditions if the boolean operator is && and the currently evaluated condition is false already. As long as conditions are true, evaluation will continue from left to right.
  2. You can generally omit checking all remaining conditions if the boolean operator is || and the currently evaluated condition is true already. Order is the same as for &&.

Short-circuit evaluation
But then, there are compilers or interpreters, like for example the Microsoft Dynamics NAV C/SIDE compiler, that always perform full boolean evaluation. That means that every condition is always checked!

In Delphi or Borland Pascal for example you can enable or disable this behaviour using a compiler directive in your code.

The default for most languages, however, is so called short-circuit boolean evaluation, which is described above.

Unexpected side-effects
However, you need to know how your compiler handles this, because it can have side effects. For example (example language, not actual syntax):

var x = 0;

function SomeFunction() : boolean
{
   x = 512;
   return true;
}

If you have a compiler that does short-cicuit evaluation, the following line would not touch x:

if false && SomeFunction() ...

If you have full boolean evaluation, after this line x would be 512, which may not be what you want!

Upvotes: 1

Federkun
Federkun

Reputation: 36954

You can try by yourself:

function thisReturnTrue() {
    echo 'executed thisReturnTrue', "\n";
    return true;
}

function thisReturnFalse() {
    echo 'executed thisReturnFalse', "\n";
    return false;
}

// executed thisReturnFalse
var_dump(thisReturnFalse() && thisReturnFalse()); // false

// executed thisReturnFalse
// executed thisReturnTrue
var_dump(thisReturnFalse() || thisReturnTrue()); // true

Upvotes: 1

malutki5200
malutki5200

Reputation: 1112

It depends on the operand.

If You use '&&' (AND):

  • 1st is true then 2nd is being tested
  • 1st is false then 2nd is NOT being tested

If You use '||' (OR):

  • 1st is true then 2nd is NOT being tested
  • 1st is false then 2nd is being tested

Upvotes: 1

Related Questions