6

JavaScript code:

var a = 1, b = 2;
a = b + (b = a) * 0;
// result a = 2, b = 1;

PHP code 1:

$a = 1;
$b = 2;
$a = $b + ($b = $a) * 0;
// result $a = 1, $b = 1;

PHP code 2:

$a = 1;
$b = 2;
$a = (int)$b + ($b = $a) * 0;
// result $a = 2, $b = 1;

What causes the difference between PHP and JavaScript assignment operators?

Is it operator precedence related?

I want to know what the reasons are. Thanks!

4
  • I suspect that neither language defines what happens if you assign to a variable and use it in the same expression. But if they do, then the difference is in order of operations. Commented Jun 26, 2014 at 9:03
  • 4
    Why are you writing such contorted code in the first place? There's no excuse for mixing assignment into the expression like this. Commented Jun 26, 2014 at 9:04
  • $a = 1; $b = 2; $a = $b + (($b = $a) * 0); Commented Jun 26, 2014 at 14:22
  • I don't think this is a real life programming issue. Voting to close. It would be best on programmers.* Commented Jun 26, 2014 at 15:12

2 Answers 2

4

No, operator precedence doesn't factor into evaluation order, therefore using assignments in complex evaluations reusing the result of the evaluation is always undefined.

From the PHP manual:

Operator precedence and associativity only determine how expressions are grouped, they do not specify an order of evaluation. PHP does not (in the general case) specify in which order an expression is evaluated and code that assumes a specific order of evaluation should be avoided, because the behavior can change between versions of PHP or depending on the surrounding code.

In short, the output of $b + ($b = $a) is undefined, since the grouping overrides precedence, and does not in anyway enforce whether the assignment occurs before fetching the left operand of the addition or after. The precedence is well defined, the execution/evaluation order is not.

Sign up to request clarification or add additional context in comments.

2 Comments

this is why we can't have anything nice in this life!!!!, How it's possible that PHP doesn't waranty an execution order. now offcially hate PHP a little more.!
Thank you for your enthusiastic response
1

To augment @NielsKeurentjes' answer with JavaScript's point of view:

(In contrast to PHP) JavaScript specifies the order of operations (operands are evaluated left-to-right), which makes it work as expected.

From the ECMAScript specification:

The production AdditiveExpression : AdditiveExpression + MultiplicativeExpression is evaluated as follows:

  1. Let lref be the result of evaluating AdditiveExpression.
  2. Let lval be GetValue(lref).
  3. Let rref be the result of evaluating MultiplicativeExpression.
  4. Let rval be GetValue(rref).

1 Comment

Good find, I couldn't find this part of the spec to augment my own answer. +1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.