1

I've been struggling to understand why this returns 252448 instead of 251248.

<?php $a = 4; echo ++$a * $a++ . $a += $a . $a *= 4; ?> 

My thinking is: ++$a * $a++ gives 25 (5*5), after that we have a incremented to 6 and added to itself, so we have 12, and 12 multiplied by 4 so we get 48.

And:

<?php $a = 4; echo ++$a * $a++ . $a *= 4 . $a += $a; ?> 

returns 254944 which is even more confusing. I thought that this would return 252448 according to similar logic.

Can anybody explain to me why that is?

4
  • The order of the calculations is not defined. So the result of ++$a * $a++ can be 25 (5*5) or 24 (6*4). Here it is possible, that ++$a or $a++ is executed first. This is a general issue in all languages. Search for "sequence point" for more details. Commented Oct 22 at 12:29
  • 1
    It says here that 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. What's the point of your question? Commented Oct 22 at 12:53
  • 4
    Although it's good to be curios, if I ever saw this in any 'real' code I would run away from the project as fast as possible! Commented Oct 22 at 13:02
  • 1
    This is a very unrealistic scenario (as Nigel has already expressed). You'd only dare to write such code if you wanted to confuse or irritate other developers. You could've used a comma for one of those dots. All of the rules of operator precedence and associativity are already provided in the PHP docs and many Stack Overflow pages have been explaining the nuances over the years. Whether this convoluted question will be valuable to future readers is unpredictable, but it can at least serve as a signpost to the many pages that explain precedence and associativity, and point to the docs. Commented Oct 22 at 23:06

1 Answer 1

6

This is PHP operator precedence and associativity

  1. ++$a / $a++ happen before *
  2. * happens before . (concatenation)
  3. All assignment operators (=, +=, *=) have very low precedence and are right associative
  4. . produces a string

In first

echo ++$a * $a++ . $a += $a . $a *= 4; 

Parsed as:

echo ( (++$a * $a++) ) . ( $a += ( $a . ( $a *= 4 ) ) ); 
  • left ( (++$a * $a++) )
    • ++$a -> 5 (and $a becomes 5)
    • $a++ -> returns 5, then $a becomes 6
    • 5 * 5 = 25
  • right ( $a += ( $a . ( $a *= 4 ) ) )
    • $a *= 4 (6 to 24) retuns 24
    • oncatenation: $a . 24 retuns 24 . 242424
    • $a += "2424 -> 24 + 2424 = 2448 (and returns 2448)

Answer: "25" . "2448" -> 252448


In second

echo ++$a * $a++ . $a *= 4 . $a += $a;

Parsed as:

echo ( (++$a * $a++) ) . ( $a *= ( 4 . ( $a += $a ) ) ); 
  • (Same as abpve) Left -> 25, $a becomes 6.
  • Right part: $a *= ( 4 . ( $a += $a ) )
    • $a += $a -> 6 + 6 = 12 ($a now 12)
    • 4 . 12 -> 412
    • $a *= "412" -> 12 * 412 = 4944

Answer: "25" . "4944" -> 254944

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

5 Comments

The result of ++$a * $a++ can be 25 (5*5) or 24 (6*4). It depends on the order of calculation, and the order is not defined.
It's always going to be 25. ++$a increments, then uses (pre-increment). $a++ uses, then increments (post-increment). So $a starts at 4, increments to 5, uses, multiplies, then increments to 6. See php.net/manual/en/language.operators.increment.php
See php.net/manual/en/language.operators.precedence.php "Example #3 Undefined order of evaluation". The evaluation order of ++$aand $a++is undefined. If the second is evaluated first, than the result is 6*4=24.
Since PHP 5.1.1 the output is always 3 not 2 - See demo. Unable to find a valid changelog for this at this point (before 5.1.1 the output was always 2 so not 2 or 3). The linked example is either invalid or is missing crucial information
What do you mean? The answer clearly indicates the order is set. "This is PHP operator precedence and associativity [...] - More information can be found in the documentation

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.