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

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=5;` — Set \$x to 5
• `\$x++ + ...` — Use, then increment
• Place \$x onto formulae stack (which is 5)
• Increment(`++`) (\$x is now 6, stack=[5])
• `... + \$x++;` — Use, then increment
• 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);

Source: stackoverflow.com

Method #2

In PHP, the operator precedence determines the order in which operators are evaluated in an expression. In the expression `\$x++ + \$x++`, the post-increment operator (`\$x++`) has a higher precedence than the addition operator (`+`), so it is evaluated first.

The first time `\$x++` is encountered, the value of `\$x` is 5, but the post-increment operator increments the value of `\$x` to 6 and returns the original value (5). The second time `\$x++` is encountered, the value of `\$x` is now 6, but the post-increment operator again increments the value of `\$x` to 7 and returns the original value (6).

So, the expression `\$x++ + \$x++` is effectively evaluated as `5 + 6`, which equals 11. That's why `\$x` will have the value of 7 after this expression is evaluated.