0

Possible duplicate: What should be the output of echo ++$a + $a++

This question targets PHP 5.x.

In the manual: http://php.net/manual/en/language.operators.precedence.php#example-111

// mixing ++ and + produces undefined behavior
$a = 1;
echo ++$a + $a++; // may print 4 or 5

On what platform or version I can get 5 as a result ? It seems 4 is the logical and correct answer every time.

I know in Java this is not undefined behavior so I wish to know if this was solved in PHP 5.x also ?

Community
  • 1
  • 1
johnlemon
  • 20,761
  • 42
  • 119
  • 178
  • Why do you want to reproduce the undefined? – j0k Feb 07 '13 at 08:49
  • "Is there any way I can reproduce the undefine behaviour in this case ?" Yes, just run the code you've shown on a PHP enabled server! – Dale Feb 07 '13 at 08:49
  • 2
    @Dale - I don't think that undefined means "random". What I understand from the manual is that different platforms might provide different results. – Álvaro González Feb 07 '13 at 08:54
  • 4
    Just like the underlying C code that powers PHP, the order of operand evaluation is not defined. The expression will always be evaluated right to left, but before the happens the operands will be reduced to a defined value. So if the `$a++` is evaluated first, the result will be 5. Which happens depends on the machine and the compiler. – DaveRandom Feb 07 '13 at 08:54
  • On what platform or version I can get 5 as a result ? – johnlemon Feb 07 '13 at 08:55
  • @danip The PHP version doesn't factor into it. I don't know off the top of my head where you could see a live demonstration of the issue, but it should be enough to know that it can happen, so don't write code like that. It's pretty opaque anyway, if you strive to write clean, clear code then it shouldn't ever be an issue. – DaveRandom Feb 07 '13 at 08:59
  • 2
    You should be writing code in such a way that there's no ambiguity in the first place, so should never be writing anything like this – Mark Baker Feb 07 '13 at 09:01
  • A statement should be sustained by proof. This is not a question about quality code. In Java this is defined and logical. In PHP it also seems the same. I need a proof this can happen. I'm not a person that just believes. – johnlemon Feb 07 '13 at 09:05
  • 2
    possible duplicate of [What should be the output of echo ++$a + $a++](http://stackoverflow.com/questions/14118282/what-should-be-the-output-of-echo-a-a) – Salman A Feb 07 '13 at 09:10
  • @DaveRandom: 5? How? Can you post an answer on the other question? – Salman A Feb 07 '13 at 09:14
  • @SalmanA Mark Baker's answer is correct, it just needs a little more textual explanation to make it canonical I think. – DaveRandom Feb 07 '13 at 09:25
  • @MarkBaker Can you add a paragraph to your answer on the other question just explaining in English what's going on, as well as the pseudo code? Then it can be called a canonical answer and that question can become a general reference for this issue. – DaveRandom Feb 07 '13 at 09:27
  • @DaveRandom - duly added, explaining both possibilities - hopefully makes it easier to understand what might be happening "under the hood" – Mark Baker Feb 07 '13 at 10:45

2 Answers2

1

Undefined behavior means that it might change in future versions of PHP, as the rules for operator precedence is ambiguous for that kind of expression.

This does not mean you'll get 4 or 5 randomly within the same version, and I'd be surprised if it ever changed in the future. It's just that the rules presented on the page are ambiguous.

MatsLindh
  • 49,529
  • 4
  • 53
  • 84
0

seems to be gone in php 5.3

tried it with:

while (true) {
    $a = 1;
    $test = ++$a + $a++;
    if ($test != 4) {
            die ($test);
    }
}
gries
  • 1,135
  • 6
  • 29