RE: question about ? :

From: Date: Wed, 19 Jun 2002 17:43:46 +0000
Subject: RE: question about ? :
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-102975@lists.php.net to get a copy of this message
> Jeez, you're right! That was dumb!! Somehow I suffered a brain-wibble that > managed to calculate 5-7 as +2 rather than -2. As I studied maths all the > way up to 1st year at university -- and do understand the rules of > precedence and associativity, honest, boss -- I really shouldn't have made > that kind of basic error. And I gave some bad information, too. Addition is indeed commutative, but subtraction isn't. Ugh. So if you have an expression that contains mixed addition and subtraction, you can't throw parens around willy-nilly and expect the same result. > Still, the principle I was trying to illustrate is still good, even if my > example was complete codswallop, so let's try again. > > Why does: > > $x = 9 - 5 + 3; // 7 > > produce a different result from: > > $x = 9 - (5 + 3); // 1 As another poster mentioned, this is because in the second example, the parens cause 5+3 to be evaluated before the subtraction. A lack of parens in the first example causes the subtraction to happen before the addition, as subtraction and addition have the same precedence, so they proceed left-to-right. Joel -- [ joel boonstra | jboonstra@gospelcom.net ]

« previous php.general (#102975) next »