Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why $x=5; $x+++$x++; equals with 11 in PHP?

Tags:

php

opcode

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.

like image 669
EmilCataranciuc Avatar asked Jun 27 '18 09:06

EmilCataranciuc


3 Answers

$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.

like image 70
Himanshu Upadhyay Avatar answered Oct 09 '22 13:10

Himanshu Upadhyay


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:

  • Set $x to 5
  • Place $x onto stack (which is 5)
  • Increment(++) ($x is now 6, stack=[5])
  • Add $x onto stack (stack=[5,6], so 5+6 -> $x=11)
  • Adding is done, that outcome is 11
  • Increment $x(++) (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);

like image 31
Martijn Avatar answered Oct 09 '22 15:10

Martijn


++ 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

like image 3
AL-zami Avatar answered Oct 09 '22 14:10

AL-zami