|
Posted by Anonymous on 04/11/07 22:55
> Yes, definately a syntax to avoid, seems unpredictable at best...
No, I would always expect what you got, it's perfectly predictable. You
should read up how the ++ operators work.
>
> <?php
> $i = 2;
> echo $i++ + 1; /// i'd expect 4, gives 3, ignores ++ increment
Does not ignore the increment, but $i++ returns the value before
incrementation, then increments. So $i++ + 1 correctly returns 2 + 1,
which is 3.
> $i = 2;
> echo $i++ + $i; // expect 5, gives 5, applies ++ increment
Correct. First $i++ is executed which returns 2, then increments a to 3,
then the + $i will be evaluated, so the expression will be 2 + 3, which
is 5.
> $i = 2;
> echo $i++ + $i++ + 1; // expect 7, get 6
> ?>
Why do you expect 7? First $i++ returns 2, increments $i to 3, so the
expression will be 2 + $i++ + 1, with $i = 3, the second $i++ will
return 3 and increment $i to 4, so we have 2 + 3 + 1, which equals 6.
Bye!
Navigation:
[Reply to this message]
|