Doc #60255 [NEW]: Inaccurate statement about foreach copying arrays
| From: | tom at kera dot name | Date: | Thu, 10 Nov 2011 12:49:23 +0000 |
| Subject: | Doc #60255 [NEW]: Inaccurate statement about foreach copying arrays | ||
| Groups: | php.doc.bugs | ||
| Request: | Send a blank email to doc-bugs+get-7409@lists.php.net to get a copy of this message | ||
From:
Operating system:
PHP version: Irrelevant
Package: Documentation problem
Bug Type: Documentation Problem
Bug description:Inaccurate statement about foreach copying arrays
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 bug report at https://bugs.php.net/bug.php?id=60255&edit=1
--
Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=60255&r=trysnapshot54
Try a snapshot (PHP 5.3): https://bugs.php.net/fix.php?id=60255&r=trysnapshot53
Try a snapshot (trunk): https://bugs.php.net/fix.php?id=60255&r=trysnapshottrunk
Fixed in SVN: https://bugs.php.net/fix.php?id=60255&r=fixed
Fixed in SVN and need be documented: https://bugs.php.net/fix.php?id=60255&r=needdocs
Fixed in release: https://bugs.php.net/fix.php?id=60255&r=alreadyfixed
Need backtrace: https://bugs.php.net/fix.php?id=60255&r=needtrace
Need Reproduce Script: https://bugs.php.net/fix.php?id=60255&r=needscript
Try newer version: https://bugs.php.net/fix.php?id=60255&r=oldversion
Not developer issue: https://bugs.php.net/fix.php?id=60255&r=support
Expected behavior: https://bugs.php.net/fix.php?id=60255&r=notwrong
Not enough info: https://bugs.php.net/fix.php?id=60255&r=notenoughinfo
Submitted twice: https://bugs.php.net/fix.php?id=60255&r=submittedtwice
register_globals: https://bugs.php.net/fix.php?id=60255&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=60255&r=php4
Daylight Savings: https://bugs.php.net/fix.php?id=60255&r=dst
IIS Stability: https://bugs.php.net/fix.php?id=60255&r=isapi
Install GNU Sed: https://bugs.php.net/fix.php?id=60255&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=60255&r=float
No Zend Extensions: https://bugs.php.net/fix.php?id=60255&r=nozend
MySQL Configuration Error: https://bugs.php.net/fix.php?id=60255&r=mysqlcfg