note 49307 rejected from language.types.array by cece
| From: | cece@php.net | Date: | Tue, 01 Feb 2005 14:36:33 +0000 |
| Subject: | note 49307 rejected from language.types.array by cece | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-84328@lists.php.net to get a copy of this message | ||
Note Submitter: Sir_Fred
----
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.