|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2004-02-26 19:29 UTC] keithm at aoeex dot com
Description:
------------
The order of operation for comparisons seems to be incorrect. The following example script will demonstrate the problem:
Reproduce code:
---------------
<?php
$x=0;
while ($x != ($x=5)){
echo "x is not equal to x";
}
?>
Expected result:
----------------
The screen should display nothing.
I would expect that the $x=5 would execute, and then php would compare $x != $x, which of course would be false, and the loop would run.
Actual result:
--------------
x is not equal to x
the loop is executed and that echo statement is displayed.
It seems that php pulls the value of $x, execute the $x=5 statement, and runs the comparison. So for a split second, you in essence have two $x variables. One which has the value of 0, and the other with the value of 5.
If you reverse the order of the operands in the condition, then it works as expected:
<?php
$x=0;
//This works fine, nothing is displayed
while (($x=5) != $x){
echo "x is not equal to x";
}
?>
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sat Nov 29 01:00:01 2025 UTC |
I agree that this is incorrect. When preforming computations in a comparison statement everything in the parentheses should be evaluated before the comparison is made. doing: for($i=0; $i < ($arr - 8);$i++) { } will have $i compared to $arr but not the value of ($arr - 8). That is wrong.