#41474 [NEW]: Clarification of array assignment behaviour
| From: | tayloj1 at uk dot ibm dot com | Date: | Wed, 23 May 2007 12:09:14 +0000 |
| Subject: | #41474 [NEW]: Clarification of array assignment behaviour | ||
| Groups: | php.doc | ||
| Request: | Send a blank email to phpdoc+get-969376405@lists.php.net to get a copy of this message | ||
From: tayloj1 at uk dot ibm dot com
Operating system:
PHP version: Irrelevant
PHP Bug Type: Documentation problem
Bug description: Clarification of array assignment behaviour
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
?>
--
Edit bug report at http://bugs.php.net/?id=41474&edit=1
--
Try a CVS snapshot (PHP 4.4): http://bugs.php.net/fix.php?id=41474&r=trysnapshot44
Try a CVS snapshot (PHP 5.2): http://bugs.php.net/fix.php?id=41474&r=trysnapshot52
Try a CVS snapshot (PHP 6.0): http://bugs.php.net/fix.php?id=41474&r=trysnapshot60
Fixed in CVS: http://bugs.php.net/fix.php?id=41474&r=fixedcvs
Fixed in release: http://bugs.php.net/fix.php?id=41474&r=alreadyfixed
Need backtrace: http://bugs.php.net/fix.php?id=41474&r=needtrace
Need Reproduce Script: http://bugs.php.net/fix.php?id=41474&r=needscript
Try newer version: http://bugs.php.net/fix.php?id=41474&r=oldversion
Not developer issue: http://bugs.php.net/fix.php?id=41474&r=support
Expected behavior: http://bugs.php.net/fix.php?id=41474&r=notwrong
Not enough info: http://bugs.php.net/fix.php?id=41474&r=notenoughinfo
Submitted twice: http://bugs.php.net/fix.php?id=41474&r=submittedtwice
register_globals: http://bugs.php.net/fix.php?id=41474&r=globals
PHP 3 support discontinued: http://bugs.php.net/fix.php?id=41474&r=php3
Daylight Savings: http://bugs.php.net/fix.php?id=41474&r=dst
IIS Stability: http://bugs.php.net/fix.php?id=41474&r=isapi
Install GNU Sed: http://bugs.php.net/fix.php?id=41474&r=gnused
Floating point limitations: http://bugs.php.net/fix.php?id=41474&r=float
No Zend Extensions: http://bugs.php.net/fix.php?id=41474&r=nozend
MySQL Configuration Error: http://bugs.php.net/fix.php?id=41474&r=mysqlcfg