note 74308 added to language.expressions
| From: | egonfreeman at gmail dot com | Date: | Wed, 04 Apr 2007 14:45:00 +0000 |
| Subject: | note 74308 added to language.expressions | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-125090@lists.php.net to get a copy of this message | ||
It is worthy to mention that:
$n = 3;
$n * --$n
WILL RETURN 4 instead of 6.
It can be a hard to spot "error", because in our human thought process this really
isn't an error at all! But you have to remember that PHP (as it is with many other high-level
languages) evaluates its statements RIGHT-TO-LEFT, and therefore "--$n" comes BEFORE
multiplying, so - in the end - it's really "2 * 2", not "3 * 2".
It is also worthy to mention that the same behavior will change:
$n = 3;
$n * $n++
from 3 * 3 into 3 * 4. Post- operations operate on a variable after it has been 'checked',
but it doesn't necessarily state that it should happen AFTER an evaluation is over (on the
contrary, as a matter of fact).
So, if you ever find yourself on a 'wild goose chase' for a bug in that
"impossible-to-break, so-very-simple" piece of code that uses pre-/post-'s, remember
this post. :)
(just thought I'd check it out - turns out I was right :P)
----
Server IP: 69.147.83.197
Probable Submitter: 85.221.134.122
----
Manual Page -- http://www.php.net/manual/en/language.expressions.php
Edit -- https://master.php.net/note/edit/74308
Del: integrated -- https://master.php.net/note/delete/74308/integrated
Del: useless -- https://master.php.net/note/delete/74308/useless
Del: bad code -- https://master.php.net/note/delete/74308/bad+code
Del: spam -- https://master.php.net/note/delete/74308/spam
Del: non-english -- https://master.php.net/note/delete/74308/non-english
Del: in docs -- https://master.php.net/note/delete/74308/in+docs
Del: other reasons-- https://master.php.net/note/delete/74308
Reject -- https://master.php.net/note/reject/74308
Search -- https://master.php.net/manage/user-notes.php