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

From: Date: Wed, 07 Oct 2009 20:00:03 +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-298747@lists.php.net to get a copy of this message
On Wed, Oct 7, 2009 at 3:58 PM, Andrew Ballard <aballard@gmail.com> wrote: > On Wed, Oct 7, 2009 at 3:42 PM, tedd <tedd.sperling@gmail.com> 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

« previous php.general (#298747) next »