RE: [PHP] Simple: Whats wrong with this?

From: Date: Thu, 18 Mar 2004 13:02:21 +0000
Subject: RE: [PHP] Simple: Whats wrong with this?
Groups: php.general 
Request: Send a blank email to php-general+get-180810@lists.php.net to get a copy of this message
On 17 March 2004 17:09, Chris W. Parker wrote: > > $var++ is a post incrementer meaning the value is updated at the next > command*. > * i'm not exactly sure how the compiler determines when to post > increment, but i think i'm correct. Not quite -- the increment is performed immediately after the access -- in fact, as part of the same operation. So: $x = 3; $y = ($x++ * 2) + $x; is likely to give you $y==10, not 9. (I say "is likely to", because you don't have any absolute cast-iron guarantees that a compiler won't, if it thinks it has good reason, commute the above to $x + ($x++ * 2), which would give you $y==9! In general, it's best to regard the value of a pre- or post-incremented variable as uncertain in the rest of the expression containing it.) Cheers! Mike --------------------------------------------------------------------- Mike Ford, Electronic Information Services Adviser, Learning Support Services, Learning & Information Services, JG125, James Graham Building, Leeds Metropolitan University, Beckett Park, LEEDS, LS6 3QS, United Kingdom Email: m.ford@leedsmet.ac.uk Tel: +44 113 283 2600 extn 4730 Fax: +44 113 283 3211

« previous php.general (#180810) next »