php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #60255 Inaccurate statement about foreach copying arrays
Submitted: 2011-11-10 12:49 UTC Modified: 2011-11-11 13:30 UTC
Votes:1
Avg. Score:4.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: tom at kera dot name Assigned: gooh (profile)
Status: Closed Package: Documentation problem
PHP Version: Irrelevant OS:
Private report: No CVE-ID: None
 [2011-11-10 12:49 UTC] 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,
)

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-11-11 13:30 UTC] 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-11 13:30 UTC] gooh@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: gooh
 [2011-11-12 13:51 UTC] nikic@php.net
Automatic comment from SVN on behalf of nikic
Revision: http://svn.php.net/viewvc/?view=revision&amp;revision=319096
Log: Followup to Doc Bug #60255: Inaccurate statement about foreach copying arrays
 [2011-11-12 14:00 UTC] nikic@php.net
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.
 
PHP Copyright © 2001-2026 The PHP Group
All rights reserved.
Last updated: Thu Oct 08 05:00:01 2026 UTC