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

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

 



On Tue, 6 Oct 2009 14:08:14 -0400, tedd.sperling@xxxxxxxxx (tedd) wrote:

>At 10:48 AM -0300 10/6/09, Martin Scotta wrote:
>>No matter how silly it can looks like (a = a++) it is still completely valid
>>code and it SHOULD run without problems.
>
>Yeah, it's a valid as:
>
>    $a = $a;
>
>and does the same thing, which is nothing.

No; it's worse, because it can be interpreted in two different ways, which is demonstrated
by the fact that it gives different results in different languages.

>If you want a statement that does something, then use:
>
>   $a = ++$a;
>
>or simply:
>
>   $a++;
>
>or
>
>   ++$a;
>
>Any of those will increment $a, whereas ($a = $a++;) does nothing.

According to Schlossnagel "Advanced PHP programming" it is better to use ++$a, because
this simply increments the variable, whereas $a++ makes a copy, and then increments the
variable, so it involves additional time and memory usage. I cannot see that it would ever
make a difference in the real world, but this is one of the tricks Schlossnagel advises
you should use when you want the fastest possible code.

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[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