Re: Whacky increment/assignment logic with $foo++ vs ++$foo

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Wed, Oct 7, 2009 at 3:58 PM, Andrew Ballard <aballard@xxxxxxxxx> wrote:

> On Wed, Oct 7, 2009 at 3:42 PM, tedd <tedd.sperling@xxxxxxxxx> wrote:
>
>> However, what I find wacky about all of this is:
>>
>>  for($i=1; $i<=10; $i++)
>>    {
>>    echo($i);
>>    }
>>
>> and
>>
>>   for($i=1; $i<=10; ++$i)
>>    {
>>    echo($i);
>>    }
>>
>> Do exactly the same thing. I would have expected the first to print 1-10,
>> while the second to print 2-10, but they both print 1-10.
>>
>> Cheers,
>>
>> tedd
>>
>>
> Not wacky at all. Think of them both this way:
>
> <?php
> $i = 1;
> while ($i <= 10) {
>     $i++;
>     echo ($i);
> }
>
> // or
>
> $i = 1;
> while ($i <= 10) {
>     ++$i;
>     echo ($i);
> }
>
> ?>
>
> Andrew
>

oops. Flip the lines inside the while, but you get the idea.

$i = 1;
while ($i <= 10) {
    echo $i;
    $i++;
}

Andrew

[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux