Re: Whacky increment/assignment logic with $foo++ vs ++$foo
| From: | Martin Scotta | Date: | Tue, 06 Oct 2009 13:48:28 +0000 |
| Subject: | Re: Whacky increment/assignment logic with $foo++ vs ++$foo | ||
| References: | 1 2 3 4 5 6 7 8 9 10 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-298648@lists.php.net to get a copy of this message | ||
On Tue, Oct 6, 2009 at 10:28 AM, Paul M Foster <paulf@quillandmouse.com>wrote:
> On Tue, Oct 06, 2009 at 08:51:17AM -0400, Bob McConnell wrote:
>
> > From: Joost [mailto:joost.t.hart@planet.nl]
> > > "Daevid Vincent" wrote:
> > >>> From: Ben Dunlap [mailto:bdunlap@agentintellect.com]
> >
> > > $a = $a++;
> >
> > I just think this is an ambiguous line of code that wasn't thought
> > through. The presence of the postfix operator makes the result
> > undefined, no matter what language you are using. It will be an accident
> > if you get the results you are expecting.
>
> The behavior of the ++ operator is the invention of Kernighan and Ritchie.
> I don't imagine they ever foresaw anyone doing something as silly as
>
> a = a++;
>
> except under the rarest of circumstances.
>
> Paul
>
> --
> Paul M. Foster
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>
No matter how silly it can looks like (a = a++) it is still completely valid
code and it SHOULD run without problems.
If we analyse any portion of code (like a simple assigment) out of context
it'll always looks like this, just silly (in this case it's really really
silly).
One point here, that nobody mention, is the side effect.
++$i has a totaly different side effect than $i++
Sometimes you don't need the side effect but in other situations it really
matter.
Does these behaves exactly?
for($i=0; $i<10; ++$i)
for($i=0; $i<10; $i++)
There is no side effect on the incremental section because the result is not
evaluated.
and what about these?
$array[ $index++ ] = $elem;
$array[ ++$index ] = $elem;
You can read more about the side effect at
http://en.wikipedia.org/wiki/Side_effect_%28computer_science%29
--
Martin Scotta