note 102154 added to language.expressions
| From: | cory at watchdogserver dot com | Date: | Sun, 30 Jan 2011 07:04:28 +0000 |
| Subject: | note 102154 added to language.expressions | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-176507@lists.php.net to get a copy of this message | ||
Quick note regarding a theory above... Yes, very good point to make about the math, however its not
an odd thing. Look closer at the logic involved.
$n = 3;
$a = $n * --$n;
Not 6 but 4? Yes, but not because of left to right evaluation!
It is 4 simply because --$ simply means to "Subtract 1 increment first before using."
Thus ... $n does become 2 resulting in 2 X 2, only because you are time-sing it by itself.
If you said...
$n=3
$x=3
$a = $x * --$n
you will get 1 subtracted from $n first then the math is 3 x 2. Again, In your case you are
time-sing it by its self is the only reason you get un-expected results.
However, place the "--" to the right of the variable like so...
$n = 3;
$a = $n * $n--;
and then you get the results you expect. Again, simply because of the rule...
--$a = subtract 1 first before using
$a-- = use it first then subtract one.
What messes people up is that they forget this rule and only remember -- means subtract 1 and ++
means add 1. But the logic is much deeper than that.
The results have nothing to do with right to left reading. It did exactly what you told it to do,
you just were not aware of what you were telling it to do.
Mr. C (27YearsExp.)
----
Server IP: 69.147.83.197
Probable Submitter: 70.126.104.89
----
Manual Page -- http://www.php.net/manual/en/language.expressions.php
Edit -- https://master.php.net/note/edit/102154
Del: integrated -- https://master.php.net/note/delete/102154/integrated
Del: useless -- https://master.php.net/note/delete/102154/useless
Del: bad code -- https://master.php.net/note/delete/102154/bad+code
Del: spam -- https://master.php.net/note/delete/102154/spam
Del: non-english -- https://master.php.net/note/delete/102154/non-english
Del: in docs -- https://master.php.net/note/delete/102154/in+docs
Del: other reasons-- https://master.php.net/note/delete/102154
Reject -- https://master.php.net/note/reject/102154
Search -- https://master.php.net/manage/user-notes.php