2

Few days ago i was on interview and I had one task to do with, to me, unknown use of increment. It goes exactly like this:

echo $x+++$x++;

I've never seen something like this before, I'm trying to figure out what it does and I'm clueless. To check it i wrote such code sample:

<?php
$x = 7;

echo $x. "<br>";
echo $x+++$x++;
?>

First line prints out what it is supposed to print out: 7.
But second line prints out 15. Why? How does this thing work? What does it mean?

Is it just me or is it similar thing as variable variable which is funny task from interviewers but is not useful in reality?

Qirel
  • 25,449
  • 7
  • 45
  • 62
spectatorx
  • 373
  • 2
  • 7
  • 22

3 Answers3

7

It might be easier if you break it up a bit.

PHP recognizes $x++ as an increment. An additional + after that is recognized as an addition. Having

$x+++$x++

would be equal to

$x++ + $x++

This is a post-increment, which means that you return the value - and then increment the value by one.

Step by step it'd be executed like this

  • Set $x = 7
  • $++ takes place, which returns the value it had before incrementing, 7, then increments $x by one
  • $x = 7 was returned, but, $x is now 8 (due to post-increment)
  • Addition of $x with the value it had before the increment, added with $x after the increment (because 7 was returned from the first $x++, but $x was naturally incremented, after the first increment $x++, $x becomes 8 - and that's now the value of the second $x before that final post-increment takes place)
  • You get the addition of 7 + 8 = 15, and print that
  • Increment $x one last time, making it $x = 9 (which is somewhat irrelevant as you don't use $x after that)

Have a look at the manual for operator precedence and increment/decrementing operators.

Qirel
  • 25,449
  • 7
  • 45
  • 62
  • 1
    Hands down the best and the most detailed answer. That's perfectly what i needed, now everything is clear. Thank you. – spectatorx Sep 04 '17 at 22:36
  • Happy to have been of assistance! Such "tricks" like these have very little real-word application, but they *can* test understanding of operator precedence - or just be a little brainteaser perhaps. ;-) It might not be logical until you split it up a little, so I understand your confusion. *Cheers!* :-) – Qirel Sep 04 '17 at 22:39
  • another intersting point would be to display $x++ . " + " . $x++ to see order of resolution – Pierre-Antoine Guillaume Sep 05 '17 at 13:26
4

It's just formatted funny. You can format it as such:

echo $x++ + $x++;

$x++ returns the value and then increments, so it's 7. Then, you add it to $x++ again. By now, $x is 8. So you get 7 + 8 = 15. If you echo $x; again, you'll get 9 (it incremented after returning the value in the last statement).

Read more about this in this great answer

ishegg
  • 9,685
  • 3
  • 16
  • 31
1

It's X++ + X++

X++ means use X and then increment so 7+8 (second time using X the value has increased by 1)

I'm not sure what they were trying to prove with such a test but they are not useful things to test (it's just knowing trivia)

exussum
  • 18,275
  • 8
  • 32
  • 65