note 33276 deleted from language.operators.comparison by sniper

From: Date: Mon, 13 Oct 2003 00:14:12 +0000
Subject: note 33276 deleted from language.operators.comparison by sniper
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-58417@lists.php.net to get a copy of this message
Note Submitter: dragon_rage4@hotmail.com ---- 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

« previous php.notes (#58417) next »