note 49307 added to language.types.array

From: Date: Sun, 23 Jan 2005 21:44:46 +0000
Subject: note 49307 added to language.types.array
Groups: php.notes 
Request: Send a blank email to php-notes+get-83827@lists.php.net to get a copy of this message
Hi, maybe someone can help me. I have NO IDEA, what is wrong in PHP. The following code (from example above) doesn't work anymore: An example from PHP documentation (above): <?php $arr1 = array(2, 3); $arr2 = $arr1; $arr2[] = 4; // $arr2 is changed, // $arr1 is still array(2, 3) // Sir_Fred: No, It's not! Not copied! // Sir_Fred: $arr1 SHOULD BE COPIED, but IT IS REFERENCED!!! ??? Why??? $arr3 = &$arr1; $arr3[] = 4; // now $arr1 and $arr3 are the same ?> I spent a lot of days for checking (because my scripts worked wrong), but always $arr1 and $arr2 are THE SAME. Somehow, the array $arr1 are referenced to $arr2, not copied! I check it on Win2000 with PHP 4.3.6 and FreeBSD with PHP 4.3.8 - result is the same! Arrays are always referenced, not copied! I'm really confused, because in older version of PHP (4.0.x) all were OK. I use PHP for last 5 years, and always $arr2 = $arr1 were copying variable, but not now. This is probably the reason, why old scripts don't work too and I've lost a lot of time. The worst is that I have no idea, how to fix it! So far, I used "&" operator for make a reference by my own will, but how ommit this auto-reference? Is this a bug in PHP (since PHP engine was rewrited in 5.0.x) or what? Thanks for any help to solve this problem. Best regards to all. ---- Manual Page -- http://www.php.net/manual/en/language.types.array.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+49307 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+49307&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+49307&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+49307&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+49307&report=yes&reason=useless Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+49307&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+49307&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#83827) next »