|
Posted by Tyno Gendo on 04/11/07 22:27
Andy Hassall wrote:
> On Wed, 11 Apr 2007 16:20:45 -0500, "Steve" <no.one@example.com> wrote:
>
>> someone asked a question in alt.php about a problem they were having with an
>> algorytm. it contained something to the effect of:
>>
>> $i = $i++;
>>
>> some example code they'd snatched somewhere. in a loop, the expected $i to
>> increment. i explained why i thought it would not - as it does not. however,
>> i want to make sure i gave a valid answer.
>>
>> anyone have input?
>
> In C and C++ that's undefined. PHP is less formally defined and so it is even
> more daft to try it.
>
> http://c-faq.com/expr/seqpoints.html
> http://c-faq.com/expr/ieqiplusplus.html
Yes, definately a syntax to avoid, seems unpredictable at best...
<?php
$i = 2;
echo $i++ + 1; /// i'd expect 4, gives 3, ignores ++ increment
$i = 2;
echo $i++ + $i; // expect 5, gives 5, applies ++ increment
$i = 2;
echo $i++ + $i++ + 1; // expect 7, get 6
?>
[Back to original message]
|