php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #28887 === does not detect assignment of built-in types
Submitted: 2004-06-23 05:24 UTC Modified: 2004-06-25 04:54 UTC
Votes:2
Avg. Score:1.0 ± 0.0
Reproduced:0 of 2 (0.0%)
From: sdavey at datalink dot net dot au Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 5.0.0RC3 OS: Windows XP Pro
Private report: No CVE-ID: None
 [2004-06-23 05:24 UTC] 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!

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-06-23 20:55 UTC] slunta at msn dot com
==
 [2004-06-25 04:54 UTC] curt@php.net
When you assign $a to $b they should be same, and when you change a value they wont be same. The === operator checks equality of type and value; it has nothing to do with variable references.

 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun May 19 04:01:33 2024 UTC