Skip to main content.

Monday, January 09, 2006

It's a simple enough question, which you should try to solve in your head: What does the following code display, and why?

<?php
  $i = 1;
  $i += $i++ + ++$i;
  echo 'i=', $i;
?>

Bonus question: Is the result always the same in other programming languages (JavaScript, Java, C#, ...; provided the syntax is adapted to the host language :))? Why (not)?

Comments

hmmm my first guess is 7...

Posted by teranex_ at Tuesday, January 10, 2006 14:22:36

I'm pretty sure it's 7, they stamped it into my brain at school:

++ has precedence over + and ++ is right associative, this gives:

++$i increments $i and then returns $i, so the second term in the sum is 2, and $i has a value of 2 at this moment
$i++ returns $i and then increments it, so the first term in the sum is 2, and $i has a value of 3 at this moment.
The sum is: 2+2=4

The finally $i+=4 results in 7.

(And if I'm completely wrong now, my teacher is gonna kill me ;) )

Posted by Lapino at Tuesday, January 10, 2006 15:50:22

This piece of code was something I remembered from a couple of years back. I now found the original posting back:
http://zengun.org/weblog/ar... (Can You Increment i?)

The output heavily depends on how expressions are evaluated.

In PHP, the result is indeed 7. My guess to how evaluation happens was like Lapino's. But it's probably wrong, since it cannot be used to explain why the following outputs "k=6": (would have expected k=5)

$j = 1;
echo 'k=', $j + $j++ + ++$j;

The reasoning in the comments at zengun (http://zengun.org/weblog/ar...) is probably more like it. My interpretation for the $j code above (continually rewriting the equation):

1) echo 'k=', 1 + $j++ + ++$j (evaluating from left to right; j=1)
2) echo 'k=', 1 + $j + ++$j (++ has precedence; j=2)
3) echo 'k=', 1 + 2 + ++$j (j=2)
4) echo 'k=', 3 + ++$j (adding one and two. This one is easy. j=2)
5) echo 'k=', 3 + $j (++ has precedence; j=3)
5) echo 'k=', 3 + 3 (j=3)
6) echo 'k=', 6

I could be completely wrong (step 2/3 feels awfully wrong), but I couldn't find another explanation for this one to return 6.

Bottom line: Don't write code like this :)

Posted by karma at Tuesday, January 10, 2006 20:39:30

The only correct answer is 7...

Posted by Jeroen at Tuesday, January 10, 2006 21:08:25

Re: "The only correct answer is 7..."

=> In PHP: indeed. In other languages: Not necessarily. For example: in JavaScript & C#, the result will be 5

Posted by karma at Wednesday, January 11, 2006 11:37:29

ouch, this hurts! :-P

Posted by moraes at Wednesday, January 18, 2006 08:30:45

I'm wondering: Isn't i++ meant to mean: Increment i by one but return the old value. Opposed to that would be ++i that incerements i and returns the new value. That used to be an old joke about C++ (the language): It's value is the same as C's.

Posted by hasko at Friday, January 20, 2006 15:35:47

Yes, that's true, so the following c-code:
int i=1;
int k=0;
k = i++;
printf("i=%d, k=%d",i,k);

Outputs: i=2 , k=1

Posted by Lapino at Sunday, January 22, 2006 03:40:30

Except in C, an expression like:

i += i++ + ++i;

is, guess what, undefined...

Posted by x mar at Thursday, April 06, 2006 06:52:51

Except in C, an expression like:

i += i++ + ++i;

is, guess what, undefined...

Posted by x mar at Thursday, April 06, 2006 06:56:21

Except in C, an expression like:

i += i++ + ++i;

is, guess what, undefined...

Posted by x mar at Thursday, April 06, 2006 07:00:51

in C

o/p for the expression

i += i++ + ++i;

is 2.
how?

Posted by GP at Wednesday, July 26, 2006 19:06:44

bollocks++

Posted by bx at Sunday, November 26, 2006 11:58:03

This code displays i= and an integer (the limit is the machine range), because it is machine dependent.

Posted by jhev at Sunday, December 03, 2006 02:15:29

The result is always the same in other programming languages, because the code is machine dependent.

Posted by jhev at Sunday, December 03, 2006 02:27:58

no..the result is not always the same.. check with JAVA

Posted by Sathya at Monday, December 11, 2006 16:24:24

Sathya, you are right, because different interpreters and or compilers can produce different code with the same programming language (machine dependentie is a goal here, but bad programming is not an alternative) and by definition different programming languages can produce different code, so, that the result is not always the same.

Posted by jhev at Saturday, December 23, 2006 09:55:35

So, the result should be always the same in other programming languages, because the code should be machine dependent.

Posted by jhev at Saturday, December 23, 2006 10:17:43

Add Comment

This item is closed, it's not possible to add new comments to it or to vote on it