Edit report at http://bugs.php.net/bug.php?id=27409&edit=1

 ID:               27409
 Comment by:       ndsharp at gmail dot com
 Reported by:      keithm at aoeex dot com
 Summary:          Comparison order of operations incorrect
 Status:           Bogus
 Type:             Bug
 Package:          Scripting Engine problem
 Operating System: Linux
 PHP Version:      5CVS-2004-02-26 (dev)

 New Comment:

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.


Previous Comments:
------------------------------------------------------------------------
[2004-02-26 19:45:45] ab...@php.net

Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

The engine evaluates the operands in the order they occur in the input,
as there is no reason to evaluate the second expression first. [The
parentheses don\'t change that.] The first expression evaluates to 0,
the second one to 5.

------------------------------------------------------------------------
[2004-02-26 19:29:49] 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";

}

?>




------------------------------------------------------------------------



-- 
Edit this bug report at http://bugs.php.net/bug.php?id=27409&edit=1

Reply via email to