According to the opcodes it should be 12. Am I getting it wrong?
number of ops: 8
compiled vars: !0 = $x
line #* E I O op fetch ext return operands
-------------------------------------------------------------------------
3 0 E > EXT_STMT
1 ASSIGN !0, 5
5 2 EXT_STMT
3 POST_INC ~2 !0
4 POST_INC ~3 !0
5 ADD ~4 ~2, ~3
6 ECHO ~4
7 7 > RETURN 1
branch: # 0; line: 3- 7; sop: 0; eop: 7; out1: -2
path #1: 0,
Edit
Also ($x++)+($x++); returns the same result (11). Actually this was the main reason for the question and opcode investigation.
$x = 5;
$a = $x++ + $x++;
the expression line will be executed like this:
1st occurrence of $x++
in the statement will increment $x
value by 1 so it will become 6 and
in 2nd occurrence, $x
will be having value 6;
So $a = 5 + 6;
So final result $a
will be 11.
It took me a few reads, but $x=5; $x++ + $x++;
works like this:
In the case of a $x++, it first 'gets used', then increased:
++
) ($x is now 6, stack=[5])++
) (which is isn't used further, but $x is now 7)Actually, in this specific example, if you would echo $x;
it would output 7. You never reassign the value back to $x, so $x=7 (you incremented it twice);
++ has higher precedence than + operator
(x++) will return the value of x first then increment it by 1
$x = 2
$x++ // return 2, then increment it to 3
x+++x++ is evaluated like the following
1. Get x value first which is 5
2. Then it will be incremented to 6
3. But first x value will be 5 because (x++) statement will return 5 first then increment the value
4. Then + operator is encountered
5. Next x will have 6 as value not 7 for the same reason (x++) will return the x value first and then increment it
6. So 5+6 is 11
7..At the end, x value will be 7
Same goes for ($x++)+($x++)
grouping operator ()
has left to right
associatevity. First ($x++)
executes first.
$x = 5
($x++) returns 5 and then increment $x by 1. Same as before.
then last ($x++) executes. It returns 6 and then increment $x to 7
so same 5+6 // 11
is returned back
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With