From: spy at spy dot zp dot ua
Operating system: FreeBSD-4.x
PHP version: 4.3.6
PHP Bug Type: Scripting Engine problem
Bug description: strange behaviour with references
Description:
------------
As shown in this example, the elements of $a become a references during
"for" cycle, although there are not have any aliases to their values.
If we make a copy ($b=$a) of an array, it becomes an array of references
too.
And only if we unset the copy ($b), engine founds that $a elements are not
really referenced by another vars.
The same result we can get with foreach($a), (because foreach works with a
copy of an array as noted in Bug #24486)
BTW, how can I make a really clean COPY of an array, including all of it
elements? And it should be useful to have a method to test if variable
have a reference(s).
Reproduce code:
---------------
$a = array(1, 2, 3);
for ($i = 0; $i < count($a); $i++) {
$x =& $a[$i];
}
$x=&$nowhere;
print "Dump 1 =>"; var_dump($a);
$b=$a;
$b['0']=6; $b['1']=7; $b['2']=8;
print "Dump 2 =>"; var_dump($a);
unset($b);
print "Dump 3 =>"; var_dump($a);
Expected result:
----------------
I don't know what to expect at all now =)
Before today I should expect all dumps like this:
Dump * =>array(3) {
[0]=>
int(1)
[1]=>
int(2)
[2]=>
int(3)
}
Actual result:
--------------
Dump 1 =>array(3) {
[0]=>
&int(1)
[1]=>
&int(2)
[2]=>
&int(3)
}
Dump 2 =>array(3) {
[0]=>
&int(6)
[1]=>
&int(7)
[2]=>
&int(8)
}
Dump 3 =>array(3) {
[0]=>
int(6)
[1]=>
int(7)
[2]=>
int(8)
}
--
Edit bug report at http://bugs.php.net/?id=28704&edit=1
--
Try a CVS snapshot (php4): http://bugs.php.net/fix.php?id=28704&r=trysnapshot4
Try a CVS snapshot (php5): http://bugs.php.net/fix.php?id=28704&r=trysnapshot5
Fixed in CVS: http://bugs.php.net/fix.php?id=28704&r=fixedcvs
Fixed in release: http://bugs.php.net/fix.php?id=28704&r=alreadyfixed
Need backtrace: http://bugs.php.net/fix.php?id=28704&r=needtrace
Need Reproduce Script: http://bugs.php.net/fix.php?id=28704&r=needscript
Try newer version: http://bugs.php.net/fix.php?id=28704&r=oldversion
Not developer issue: http://bugs.php.net/fix.php?id=28704&r=support
Expected behavior: http://bugs.php.net/fix.php?id=28704&r=notwrong
Not enough info: http://bugs.php.net/fix.php?id=28704&r=notenoughinfo
Submitted twice: http://bugs.php.net/fix.php?id=28704&r=submittedtwice
register_globals: http://bugs.php.net/fix.php?id=28704&r=globals
PHP 3 support discontinued: http://bugs.php.net/fix.php?id=28704&r=php3
Daylight Savings: http://bugs.php.net/fix.php?id=28704&r=dst
IIS Stability: http://bugs.php.net/fix.php?id=28704&r=isapi
Install GNU Sed: http://bugs.php.net/fix.php?id=28704&r=gnused
Floating point limitations: http://bugs.php.net/fix.php?id=28704&r=float