Doc #60255 [Opn->Csd]: Inaccurate statement about foreach copying arrays
| From: | gooh@php.net | Date: | Fri, 11 Nov 2011 13:30:39 +0000 |
| Subject: | Doc #60255 [Opn->Csd]: Inaccurate statement about foreach copying arrays | ||
| References: | 1 | Groups: | php.doc.bugs |
| Request: | Send a blank email to doc-bugs+get-7416@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
Updated by: gooh@php.net
Reported by: tom at kera dot name
Summary: Inaccurate statement about foreach copying arrays
-Status: Open
+Status: Closed
Type: Documentation Problem
Package: Documentation problem
PHP Version: Irrelevant
-Assigned To:
+Assigned To: gooh
Block user comment: N
Private report: N
New Comment:
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.
Previous Comments:
------------------------------------------------------------------------
[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