3

I was wondering how php processes if statements. If i were to have something such as:

if (isset($_GET['foo']) && $_GET['foo'] != $bar)

If foo isn't set, would it then drop out of the if straight away (as it is an 'and' statement so it can't succeed anyway) or would it also check the second part, rather pointlessly?

nfechner
  • 17,295
  • 7
  • 45
  • 64
Flibx
  • 95
  • 1
  • 7
  • search for php lazy evaluation on SO, you will get your long answer, for short one, the && operator is short circuited meaning it will not evaluate the second condition if it finds that first one has failed. Check [this link](http://stackoverflow.com/questions/3220919/php-short-circuit-lazy-evaluation-where-is-it-in-the-php-net-manual) – wadkar Aug 08 '11 at 11:41

8 Answers8

4

What you're describing is known as "short-circuit evaluation".

Most languages work this way, including PHP, so they will evaluate an expression until they are certain of the result, and then stop, so the remainder of the expression would not be evaluated.

As you say, this is the most efficient approach.

However, it can potentially throw a spanner in the works for inexperienced programmers, who nay try something like this:

if(doFirstProcess() && doSecondProcess() {
    print "both processes succeeded";
}

In this case, the programmer is expecting both functions to be called, but if the first one returns false, then the second one will not be executed, as the program already knows enough to be certain of the final result of the expression, so it short-circuits the remainder of the expression.

There are a few languages which don't do short-circuit evaluation. VB6 was one example (back in the day). I don't know about VB.Net, but since it's evolved from VB6, I would suspect it would be similar. But aside from that, all other languages that I've worked with have used short-circuit evaluation, including PHP.

There is a section in the PHP manual about this here: http://www.php.net/manual/en/language.operators.logical.php

And you can read more on short circuit evalution here: http://en.wikipedia.org/wiki/Short-circuit_evaluation

Hope that helps.

Spudley
  • 166,037
  • 39
  • 233
  • 307
  • As an aside, VB.NET at some point added extra operators to provide short-circuiting. `And` and `Or` still don't short-circuit, but [`AndAlso`](http://msdn.microsoft.com/en-us/library/cb8x3kfz.aspx) and [`OrElse`](http://msdn.microsoft.com/en-us/library/ea1sssb2.aspx) do. – Aether Aug 08 '11 at 13:21
  • @Aether - thanks for that... gosh, I'm glad I never had to move to VB.Net. – Spudley Aug 08 '11 at 13:38
  • Thanks for the great explanation on this. I was just tinkering with a PHP script and wondered this exact question. Now I know and you get an upvote (or whatever stack calls them). – Cloudkiller Sep 24 '13 at 18:36
3

It's known as short-circuit:

  • && and and operators is executed from left to side
  • If left side is considered false, no reasons to check right side, so it's omitted, false returned

  • || and or operators is executed from left to side too

  • If left side is considered true, no reasons to check right side, so it's omitted, true returned

Manual example:

// foo() will never get called as those operators are short-circuit

$a = (false && foo());
$b = (true  || foo());
$c = (false and foo());
$d = (true  or  foo());
RiaD
  • 46,822
  • 11
  • 79
  • 123
2

it will leave the if statement after the first expression evaluates to false because this statement can never be true if the first one is false and they are combinded via AND

you can check this very easily. If it wouldn't be like I said, you would get a notice that $_GET['foo'] is not defined

Fender
  • 3,055
  • 1
  • 17
  • 25
0

If the first part is false, it stops the if.

Jean-Philippe Leclerc
  • 6,713
  • 5
  • 43
  • 66
0

Certain operators, most notably && and || are so-called short-circuit operators, meaning that if the result of the operation is clear from the first operand (false or true, respectively), the second operand does not get evaluated.

Edit: Additionally, operands are guaranteed to be evaluated in order, this is not always true of other operators.

Williham Totland
  • 28,471
  • 6
  • 52
  • 68
0

Will go out after the first statement. you can test it:

will print 1:

if(1==1 && $a=1 == 1){

}
print $a;

Will not print a thing:

if(1==2 && $a=1 == 1){

}
print $a;
fatnjazzy
  • 6,070
  • 12
  • 57
  • 83
0

&& does short-circuit (i.e. returns false as soon as one condition fails).

If it doesn't, then having the isset would be pointless — it exists to prevent errors when trying to compare an undefined value to a string.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
0

If the first check if (isset($_GET['foo']) returns false, the second part will not even be looked into anymore.

Jules
  • 7,148
  • 6
  • 26
  • 50