Evaluation order

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

when I run:

$a = 1;
$b = 2;
var_dump($a == ($a = $b));

I get "true".

Though I accept that this may be the desired and expected result, I'm
curious from which part of the documentation this can be deduced from.

At first glance I'd say that $a should already have been evaluated to
1 and ($a = $b) to 2 when the comparison takes place, so it should be


Re: Evaluation order

Andr Hnsel wrote:
Quoted text here. Click to load it

I would suspect due to precedence of order of how PHP set variables.

It sets the first $a when the second one has been changed, before it was

Just as if it was one line down.


Re: Evaluation order

Andr Hnsel wrote:
Quoted text here. Click to load it

You are relying on a side-effect, that is the value of a variable being
changed *during* the evaluation of an expression containing that variable.
It's probably not specified anywhere.

In fact in the C++ language such a construct is specifically specified as
being undefined. You can not, and should not, reliably predict if the
outcome will be true or false. It's entirely up to the way the expression
evaluator is constructed. Only when one gets to the end of a statement (the
; above) is everything settled down and well defined and that too is quite
well specified in the language. Quite a large percentage of posts over in
the C++ groups are about such things being officially specified as being

Perhaps the simplest example is
$i = 1;
$i = $i++;
What value does $i now have? Try it and see, you may be surprised. Then
*you* tell *me* why you got the surprising result :-)

Quoted text here. Click to load it

That is also the way I would construct an expression parser but it appears
that $a is not "evaluated" at all until what is inside the inner parentheses
is evaluated. Or perhaps $a is not evaluated at all but merely referenced
when it comes time to evaluate the == operator. You would need to go and
look at the PHP source code to find out.


Re: Evaluation order

Andr Hnsel wrote:
Quoted text here. Click to load it

The PHP spec only specifies the order of evaluation of the operators -
not the operands.  Using an operand in the same expression as you are
changing it has undefined results.

Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.

Re: Evaluation order

André Hänsel wrote:

Quoted text here. Click to load it


Most compilers will evaluate both sides of an operand (say, "==") until both
of them can be stored in a register. As the first operand is already a
variable, it doesn't need to be touched... the evaluation of the second
operator ( "($a=$b)" ) will take place, and that will modify the *value*,
not the *address* or register of $a. So the updated value of $a reaches the
comparison at last.

Really, this stuff becomes crystal clear when you take a course on

Iván Sánchez Ortega -ivan-algarroba-sanchezortega-punto-es-

Jabber:ivansanchez@jabber.org ; ivansanchez@kdetalk.net

Site Timeline