ID:               28887
 Comment by:       slunta at msn dot com
 Reported By:      sdavey at datalink dot net dot au
 Status:           Open
 Bug Type:         Scripting Engine problem
 Operating System: Windows XP Pro
 PHP Version:      5.0.0RC3
 New Comment:

==


Previous Comments:
------------------------------------------------------------------------

[2004-06-23 05:24:35] sdavey at datalink dot net dot au

Description:
------------
Hi,

For PHP5RC3, the assignment operator only performs the copy when one of
the variables is later changed.  I guess this is done for performance
reasons, but this can lead to problems if you are using the ===
operator.

Let some code explain.

<?
$a = 1;
$b = $a;
if ($b === $a) print "b === a!";  /* this prints */
$a = 2;
if ($b === $a) print "b still === a.\n"; /* this doesn't */
?>

My assignment of $a to $b should have resulted in $a and $b being
different, but the first === check shows them to be the same.    Only
after I change $a and $b does the copy occur, and they become
different.

If the copy is delayed for performance reasons, the === operator should
detect the delayed copy so it returns the correct answer to the
programmer.   For example, a programmer may try the following, thinking
the two variables are references to the same data, when in fact they may
not be, depending on whether they have been changed since the assignment
took place:
<?
if ($a === $b) {
  $a = 1;  // no need to change $b
}
?>

Hope this makes sense.

I think this has been the behaviour of PHP since the late 4.3.* range
based on a comment in the PHP manual, but IMHO the programmer shouldn't
have to deal with internal PHP optimiations that may change silently in
the future.

I'm interested to see whether the PHP Dev Team see this as a feature or
a bug.

Scott

Reproduce code:
---------------
<?
$a = 1;
$b = $a;
if ($b === $a) print "b === a!";  /* this prints */
$a = 2;
if ($b === $a) print "b still === a.\n"; /* this doesn't */
?>

Expected result:
----------------
No output.

Actual result:
--------------
b === a!


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


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

Reply via email to