> -----Original Message----- > From: Robert Cummings [mailto:robert@xxxxxxxxxxxxx] > Sent: Friday, October 02, 2009 2:12 PM > To: Daevid Vincent > Cc: php-general@xxxxxxxxxxxxx > Subject: Re: Whacky increment/assignment logic with > $foo++ vs ++$foo > > Daevid Vincent wrote: > > <?PHP > > $num = 123; > > $num = $num++; > > print $num; //this prints 123 and not 124 ?!! > > > > $num = 123; > > $num = ++$num; > > print $num; //this prints 124 as expected > > > > $num = 123; > > $num++; > > print $num; //this prints 124 as expected > > > > $num = 123; > > print $num++; //this prints 123 and not 124 ?!! > > print $num++; //this NOW prints 124 > > ?> > > > > So then I read the manual because I think I'm loosing my > mind and perhaps > > it's backwards day and nobody told me: > > http://us3.php.net/manual/en/language.operators.increment.php > > > > I'm baffled as to the reasoning behind: > > "$a++ :: Post-increment :: Returns $a, then increments $a by one." > > > > Why would you EVER want $num = $num++; to give you back the > value you > > already had? Even if we did $foo = $bar++; I would still > logically (and > > common sensely) expect $foo to be the increment of $bar! > > > > It also seems counter-intuitive, as I was always lead to > believe everything > > is processed on the right of an equals sign and then > assigned back to the > > left side of the equals sign. In this case, it does a > mixture of both. > > > > Can someone PLEASE explain why the developers of PHP chose > this seemingly > > whacky logic? > > This logic is almost universal in modern programming languages. Some > people do the following: > > <?php > > $index = 1; > foreach( @items as @item ) > { > echo '<tr>' > .'<td>'.($index++).'</td>' > .'<td>'.$item.'</td>' > .'</tr>'; > } > > ?> > > Some people consider it weird to start at 0 for the above > index, because > the index is only used when it is 1 or more. But the > equivalent of the > above is the following: > > <?php > > $index = 0; > foreach( @items as @item ) > { > echo '<tr>' > .'<td>'.(++$index).'</td>' > .'<td>'.$item.'</td>' > .'</tr>'; > } > > ?> My problem isn't with $foo++ vs ++$foo per say. I use pre/post all the time. My issue is that I see no reason to do the ASSIGNMENT FIRST and THEN INCREMENT. That's just counter intuitive. In the case of $foo = $num++, everything to the right of the = should be computed FIRST and THEN handed off to the left side. This particular expression (and I'm unaware of any other PHP expression that works this way) chooses to do some "FM" (f'n magic) and do an assignment FIRST and THEN increment. All of this is just an academic discussion at this point, as I'm sure there is WAY too much code out there to even consider changing this behaviour to a more sane one. And I'm told it mimics other languages (wright or wrong, it is what it is). Personally I've never (in almost 20 years) done an assignment like "$foo = $foo++" as I always use just "$foo++" or "$foo += 1" or something, hence the reason today is the day a co-worker stumbled upon this and was as confused as I was until I figured it out. D. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php