0
php > $a = 4;
php > echo $a+++$a++;
php > 9

Why is the result equal to 9 not 8 ?

a++ will increment the value of $a, but return the original value that I held before being incremented. So $a++ return 4 and $a return 4, the result should be 8 = 4 + 4 ?

halfer
  • 19,824
  • 17
  • 99
  • 186
Aya
  • 158
  • 10

1 Answers1

4

The first time you get a return value from $a++ the value will be 4. But the second time it will be 5, as you already incremented it just before:

9 = 4 + 5

The final value of $a is 6, as it has been incremented twice.

dancingCamel
  • 206
  • 2
  • 8
  • Your answer is wrong, this question must reopened and the right explanation must be provided. And this question is not a duplicate of the "Pre-incrementation vs. post-incrementation" question. – user1597430 Dec 20 '20 at 00:41
  • I believe my explanation is correct. Here is a previous question about c/c++ that is asking something similar and notes the effect of 'use-then-change' post-incrementation: https://stackoverflow.com/questions/1812990/incrementing-in-c-when-to-use-x-or-x/33580329#33580329 . There is also a comment in the php docs related to this: https://www.php.net/manual/en/language.operators.increment.php – dancingCamel Dec 20 '20 at 23:50
  • As I said, you are completely wrong. Look at https://www.php.net/manual/en/language.operators.precedence.php, example #2. – user1597430 Dec 21 '20 at 01:29
  • It would be useful if instead of stating "you are completely wrong" you offered some explanations as to why that might be the case. The example #2 in the link merely confirms my explanation $a = 1 echo $a + $a++ will print either 2 or 3. EITHER the left $a is evaluated first: echo 1 + $a++ where $a =1 still. Thus echo 1 + 1++ which will echo 2 and then increment $a to 2. OR you evaluate the $a++ first. echo $a + ($a++) ==> echo 2 + 1 // prints 3. In the OP both sides will increment and so will either result in 9 = 4 + 5 OR 9 = 5 + 4. The answer is 9 regardless of the order of operation. – dancingCamel Dec 21 '20 at 09:31
  • @user1597430 this is the correct answer. As stated by @dancingCamel, there are two `$a++` operators here so it doesn't matter which is evaluated first. The end result will be either `4 + 5` or `5 + 4`, either way the answer will be 9. Going into more detail, the `$a++` is a "Post Increment" that will first return `$a` then increment. As a result the formula becomes `echo $a + ($a + 1)`. The first `$a++` will return the value `$a` before increment, the second `$a++` will return `$a + 1` as `$a` has been incremented once. – Ewan Dec 21 '20 at 15:32
  • @Ewan, you both don't understand that `=` is also an operator that can be executed before any `++`. This is exactly what example #2 tells you in the URL above. – user1597430 Dec 22 '20 at 00:43
  • @user1597430 while true that is irrelevant in this example as the statement in question is `echo $a++ + $a++`. This answers why the answer is `9`. If you have a different answer please post it with a clear explanation rather than merely posting that it is wrong with no explanation as to why. – Ewan Dec 22 '20 at 07:16