note 33276 added to language.operators.comparison

From: Date: Fri, 20 Jun 2003 16:12:00 +0000
Subject: note 33276 added to language.operators.comparison
Groups: php.notes 
Request: Send a blank email to php-notes+get-50761@lists.php.net to get a copy of this message
In response to what bishop put back in October 2002, I thought that it needed clarifying on the last example he had. $i = 7; array ($i) == array($i++) This will return TRUE due to PHP's C/C++ origins. In C++ syntax, you can increment using $myVar++; or ++$myVar; The first example will increment the variable after an operation done to it, and the second will increment before the operation. To demonstrate: $myVar = 3; print $myVar++; // This will print 3 print $myVar; // This will print 4 Now, if we could use the ++$myVar syntazt, you could have the following code: $myVar = 3; print ++$myVar; // This will print 4 (theoretically) print $myVar; // This will print 4 again Also, the following code works in PHP 4: $myVar = 3; if ($myVar == $myVar++) { print "They are equal"; // This will print. $myVar wasn't incremented until after the test } if ($myVar == $myVar++) { print "They are equal"; // This will not print. $myVar had already been incremented beforehand to 4. } print $myVar; // This will print 5 One last word of advice, don't fall for the assignment in an if statement pit. It still gets me every once in a while: $myVar = 3; if ($myVar = 5) // Single equals { print "This is true"; // This will print } print $myVar; // This prints 5 ---- Manual Page -- http://www.php.net/manual/en/language.operators.comparison.php Edit Note -- http://master.php.net/manage/user-notes.php?action=edit+33276 Delete Note -- http://master.php.net/manage/user-notes.php?action=delete+33276&report=yes Reject Note -- http://master.php.net/manage/user-notes.php?action=reject+33276&report=yes

« previous php.notes (#50761) next »