#28887 [Opn->Bgs]: === does not detect assignment of built-in types

From: Date: Fri, 25 Jun 2004 02:54:52 +0000
Subject: #28887 [Opn->Bgs]: === does not detect assignment of built-in types
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-61339@lists.php.net to get a copy of this message
ID: 28887 Updated by: curt@php.net Reported By: sdavey at datalink dot net dot au -Status: Open +Status: Bogus Bug Type: Scripting Engine problem Operating System: Windows XP Pro PHP Version: 5.0.0RC3 New Comment: 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. Previous Comments: ------------------------------------------------------------------------ [2004-06-23 20:55:49] slunta at msn dot com == ------------------------------------------------------------------------ [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

« previous php.bugs (#61339) next »