note 102154 deleted from language.expressions by danbrown

From: Date: Mon, 31 Jan 2011 13:44:48 +0000
Subject: note 102154 deleted from language.expressions by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-176533@lists.php.net to get a copy of this message
Note Submitter: cory at watchdogserver dot com ---- 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.)

« previous php.notes (#176533) next »