note 93847 added to language.operators.increment
| From: | daevid at daevid dot com | Date: | Fri, 02 Oct 2009 21:11:16 +0000 |
| Subject: | note 93847 added to language.operators.increment | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-161240@lists.php.net to get a copy of this message | ||
<?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.
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.
----
Server IP: 209.41.74.194
Probable Submitter: 206.169.197.253
----
Manual Page -- http://www.php.net/manual/en/language.operators.increment.php
Edit -- https://master.php.net/note/edit/93847
Del: integrated -- https://master.php.net/note/delete/93847/integrated
Del: useless -- https://master.php.net/note/delete/93847/useless
Del: bad code -- https://master.php.net/note/delete/93847/bad+code
Del: spam -- https://master.php.net/note/delete/93847/spam
Del: non-english -- https://master.php.net/note/delete/93847/non-english
Del: in docs -- https://master.php.net/note/delete/93847/in+docs
Del: other reasons-- https://master.php.net/note/delete/93847
Reject -- https://master.php.net/note/reject/93847
Search -- https://master.php.net/manage/user-notes.php