2

Possible Duplicates:
PHP - and / or keywords
PHP shorthand syntax

Quick question. I keep seeing shorthand expressions in libraries around the place, and despite having been a PHP developer for over 3 years, I struggle to quite see how the following would evaluate.

What exactly does the PHP interpreter do with the following shorthand lines of code when it encounters them?

<?php
    defined('foo') OR exit('foo is not defined!');

    $foo AND $this->bar();

I'm guessing that it' obviously conditional execution - i.e. the second statement won't get executed unless the first bit is true... but the use of the bitwise operators confuse me a bit.

Can someone elaborate?

Thanks :)

Community
  • 1
  • 1
George
  • 901
  • 9
  • 23
  • @Gordon - no not really. I know that && is the same is AND, likewise with || and OR. I was enquiring about the shorthand logic - which has now been pointed out to me as short circuit evaluation. – George Jun 20 '11 at 13:49
  • 1
    well, how about http://stackoverflow.com/questions/2475047/php-shorthand-syntax then? – Gordon Jun 20 '11 at 13:52
  • someone just linked me to that. what exactly is your issue? – George Jun 20 '11 at 13:53
  • I'm just pointing out possible duplicates. On a sidenote, the guy who answered with that link shouldnt have answered with that link. It should have been a comment or a closevote. That's why I copied it up here and flagged his answer for not being an answer. It's all just housekeeping. – Gordon Jun 20 '11 at 13:59

4 Answers4

4

I believe that this is a form of short-circuit evaluation: http://en.wikipedia.org/wiki/Short-circuit_evaluation

Essentially, when evaluating an entire expression, the interpreter short circuits once it is certain of the result. For example:

true OR do_something();

This expression never calls do_something() because the first operand of OR is true, so the entire expression must be true.

jncraton
  • 9,022
  • 3
  • 34
  • 49
2

Much less elaborate than you think. The or keyword is the same as ||, except it has lower precedence. See the corresponding PHP page for details.

The actual execution of this statement relies on a separate concept involving logical operators: short circuiting. This means that, in the case of or, the second statement is not evaluated if the first statement turns out to be true. (Since in a conditional, for instance, the entire condition would return true without ever needing to see the second half.)

TNi
  • 16,070
  • 3
  • 22
  • 20
2

Back when I was learning PHP I remember reading the 'do or die' style commands and not fully understanding them at the time, the classic example is:

mysql_connect() or die('couldn\'t connect');

Bear in mind that the conditions will only run if they're required, for example:

if (a == b && b == c)

Here b == c will only be tested if a == b. The same theory applies to or:

if (a == b || b == c)

Now b == c will only be tested if a != b.

In this case you are relying on this order to run a command (exit or $this->bar()) in certain conditions.

Be Aware... exit() is a bad idea in this circumstance - if you exit('something went wrong') there's nothing anyone can do to hide this error from the user, also it's likely to issue a 200 OK HTTP status where a 500 Internal Server Error would be much more appropriate, I would consider something more like:

defined('foo') OR throw new Exception('foo is not defined!');

Here you have a chance to either catch the Exception or at least let PHP catch it and issue a 500 status.

Mat

MatCarey
  • 2,409
  • 1
  • 16
  • 12
  • Hey Mat, thanks for the answer. Yeah I know it's not the best way at all. I actually just pulled the example from most files in the core of CodeIgniter... which is a heavily PHP 4 oriented framework. – George Jun 20 '11 at 13:50
1

Sometimes you have a function that looks something like:

function test()
{
    $foo = TRUE;
    $bar = FALSE;

    return $foo && $bar;
}

In this case, if some does:

 $bla = test();
 if($bla) {
   ... do something
 }

The "...do something" will not get executed, because the function as a whole returned false.

return $foo && $bar means that both variables have to be true in order for the function to return true.

On the other hand, if it stated: return $foo || $bar; then the function would return true, because at least one variable is true.

djdy
  • 6,779
  • 6
  • 38
  • 62