From:             sdavey at datalink dot net dot au
Operating system: Windows XP Pro
PHP version:      5.0.0RC3
PHP Bug Type:     Scripting Engine problem
Bug description:  === does not detect assignment of built-in types

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 bug report at http://bugs.php.net/?id=28887&edit=1
-- 
Try a CVS snapshot (php4):  http://bugs.php.net/fix.php?id=28887&r=trysnapshot4
Try a CVS snapshot (php5):  http://bugs.php.net/fix.php?id=28887&r=trysnapshot5
Fixed in CVS:               http://bugs.php.net/fix.php?id=28887&r=fixedcvs
Fixed in release:           http://bugs.php.net/fix.php?id=28887&r=alreadyfixed
Need backtrace:             http://bugs.php.net/fix.php?id=28887&r=needtrace
Need Reproduce Script:      http://bugs.php.net/fix.php?id=28887&r=needscript
Try newer version:          http://bugs.php.net/fix.php?id=28887&r=oldversion
Not developer issue:        http://bugs.php.net/fix.php?id=28887&r=support
Expected behavior:          http://bugs.php.net/fix.php?id=28887&r=notwrong
Not enough info:            http://bugs.php.net/fix.php?id=28887&r=notenoughinfo
Submitted twice:            http://bugs.php.net/fix.php?id=28887&r=submittedtwice
register_globals:           http://bugs.php.net/fix.php?id=28887&r=globals
PHP 3 support discontinued: http://bugs.php.net/fix.php?id=28887&r=php3
Daylight Savings:           http://bugs.php.net/fix.php?id=28887&r=dst
IIS Stability:              http://bugs.php.net/fix.php?id=28887&r=isapi
Install GNU Sed:            http://bugs.php.net/fix.php?id=28887&r=gnused
Floating point limitations: http://bugs.php.net/fix.php?id=28887&r=float

Reply via email to