Operator precedence or how does 'or die()' work.

In the old school PHP code you can often see a statement like this:

$result = some_function () OR some_other_function ();

Sadly, but most of time it features die() as some_other_function() , which is extremely bad practice. Every time you see it, replace die() with trigger_error() , which will give you exactly the same result on your local system but will make the code ready for going live. Read more on the proper error reporting...

In this statement, some_other_function() would be called only if some_function() fails (or, strictly speaking, returns an empty result).

But how does it actually work? Here you go:

A smart interpreter

A code like this

some expression 1 OR some expression 2 ;

is just a PHP expression. Exactly the same as

$variable = some expression ;

and OR operator is just like = (or any other - > , * , . and such) operator.

Most of time we would like to assign the result of OR operator to some variable (note that here we have 2 operators: = and OR ):

$variable = ( some expression 1 OR some expression 2 );

which will give us a Boolean value in $variable . But assignment operator is not obligatory here. We can always omit the leftmost part, leaving only

some expression 1 OR some expression 2 ;

and it will cause no parse error.

And here comes the key part: because OR operator will return TRUE if one of the 2 operands is TRUE, a smart interpreter wouldn't run the second operand at all, if first one already evaluated to TRUE!

So, this is the trick:

some expression 1 OR some expression 2 ;

means "execute some expression 2 only if some expression 1 returned FALSE "

Operator precedence

Now let's get back to

$variable = some expression 1 OR some expression 2 ;

There is a thing called operator precedence. And = operator has a higher precedence than OR and so it will be executed first. Thus, this expression can be written as

( $variable = some expression 1 ) OR some expression 2 ;

So now you can conclude how the whole thing works

some expression 1 gets evaluated first.

gets evaluated first. then = operator gets executed, and the result of some expression 1 assigned to $variable .

operator gets executed, and the result of assigned to . then PHP proceeds to the execution of OR statement and here goes the trick: if value of $variable is equal to TRUE , then PHP won't fire the rightmost one. but if it is equal to FALSE , the rightmost expression gets executed and we have an error triggered (if trigger_error() is used as some expression 2).

statement and here goes the trick:

Note that logical operators in PHP can be used with any data type, not only Boolean values, due PHP's automatic type conversion:

FALSE would be assumed for 0 , "0" (a string contains literal 0), NULL and empty array.

, (a string contains literal 0), and empty array. TRUE would be assumed for everything else, including values like -1 , " " (a space character), array(0) (an array with a single item equal to 0).

The difference between || and OR

On a side note, now you can tell why we are using || instead of OR when we want a boolean result but not a trick. || has lesser precedence than = and thus

$variable = some expression 1 || some expression 2 ;