Doc #60255 [Com]: Inaccurate statement about foreach copying arrays
| From: | nikic@php.net | Date: | Sat, 12 Nov 2011 14:00:47 +0000 |
| Subject: | Doc #60255 [Com]: Inaccurate statement about foreach copying arrays | ||
| References: | 1 | Groups: | php.doc.bugs |
| Request: | Send a blank email to doc-bugs+get-7425@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=60255&edit=1
ID: 60255
Comment by: nikic@php.net
Reported by: tom at kera dot name
Summary: Inaccurate statement about foreach copying arrays
Status: Closed
Type: Documentation Problem
Package: Documentation problem
PHP Version: Irrelevant
Assigned To: gooh
Block user comment: N
Private report: N
New Comment:
When and what exactly foreach copies is a little bit complicated and requires some understanding of
internals, so we decided to remove the note altogether.
Previous Comments:
------------------------------------------------------------------------
[2011-11-12 13:51:05] nikic@php.net
Automatic comment from SVN on behalf of nikic
Revision: http://svn.php.net/viewvc/?view=revision&revision=319096
Log: Followup to Doc Bug #60255: Inaccurate statement about foreach copying arrays
------------------------------------------------------------------------
[2011-11-11 13:30:39] gooh@php.net
This bug has been fixed in the documentation's XML sources. Since the
online and downloadable versions of the documentation need some time
to get updated, we would like to ask you to be a bit patient.
Thank you for the report, and for helping us make our documentation better.
------------------------------------------------------------------------
[2011-11-10 12:49:23] tom at kera dot name
Description:
------------
The following text in a note on http://php.net/manual/en/control-structures.foreach.php
is, I believe, inaccurate:
"Unless the array is referenced, foreach operates on a copy of the specified array and not the
array itself."
This implies that the snippet seen below will never work on the original array; however, everybody
knows that it does!
This passage was quoted by teaching material, causing -- if my analysis is correct -- students being
taught to use alternative loop constructs that are not best suited for the task at hand.
This passage should just be removed.
Test script:
---------------
<?php
$someArray = Array(1, 2, 3);
// +---> array not referenced
// |
// | +---> value referenced
// | | for demonstration
// | |
foreach ($someArray as &$value) {
$value = 3;
}
var_export($someArray);
?>
Expected result:
----------------
array (
0 => 3,
1 => 3,
2 => 3,
)
Actual result:
--------------
// Actual result:
array (
0 => 3,
1 => 3,
2 => 3,
)
// Result implied by documentation:
array (
0 => 1,
1 => 2,
2 => 3,
)
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=60255&edit=1