php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #41474 Clarification of array assignment behaviour
Submitted: 2007-05-23 12:09 UTC Modified: 2007-08-17 08:12 UTC
From: tayloj1 at uk dot ibm dot com Assigned:
Status: Wont fix Package: Documentation problem
PHP Version: Irrelevant OS:
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: tayloj1 at uk dot ibm dot com
New email:
PHP Version: OS:

 

 [2007-05-23 12:09 UTC] tayloj1 at uk dot ibm dot com
Description:
------------
Ref:   http://uk.php.net/manual/en/language.types.array.php

Current PHP manual says: 
"You should be aware that array assignment always involves value copying. It also means that the internal array pointer used by current() and similar functions is reset. You need to use the reference operator to copy an array by reference. "

This statement can be misunderstood and should be clarified as follows: 

" You should be aware that array assignment <delete word 'always'> involves value copying. It also means that the internal array pointer used by current() and similar functions is reset. You need to use the reference operator to copy an array by reference. When  the array is copied by value, elements in the array are copied by reference if they are already references. An element in an array will be a reference if it was created by a reference assignment or if a reference has been created to the element."

Example: 

<?php
   $a = array (1,2) ;
   $b = &$a[0] ;      // make $a[0] part of ref set
   $c = $a ;
   $a[0] = 3 ;
   $a[1] = 4 ;
   echo $c[0] ; // outputs 3 because $c[0] was copied by reference
   echo "\n" ;
   echo $c[1] ; // outputs 2 (not 4) because $c[1] was copied by value
?>



Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-08-17 08:12 UTC] vrana@php.net
Is has nothing to do with arrays - that's the way how assignment works even with normal variables.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu May 30 03:01:30 2024 UTC