Doc #71458 [NEW]: foreach docs should expand upon the references warning
| From: | requinix@php.net | Date: | Tue, 26 Jan 2016 19:51:41 +0000 |
| Subject: | Doc #71458 [NEW]: foreach docs should expand upon the references warning | ||
| Groups: | php.doc.bugs | ||
| Request: | Send a blank email to doc-bugs+get-13140@lists.php.net to get a copy of this message | ||
From: requinix@php.net
Operating system:
PHP version: Irrelevant
Package: Scripting Engine problem
Bug Type: Documentation Problem
Bug description:foreach docs should expand upon the references warning
Description:
------------
Every other month there's a bug report about how having two foreach
loops, the first with a reference and the second without, results in
unexpected behavior. (The bug system suggested four of them to me now as
possible duplicates of this.)
While there is a warning in the documentation,
"Warning
Reference of a $value and the last array element remain even after the
foreach loop. It is recommended to destroy it by unset()."
an example would be helpful to illustrate what the effects of that can
look like to a developer. Plus it would make the warning box larger and
more visible.
An example such as:
Test script:
---------------
<?php
$array = [1, 2, 3, 4, 5];
foreach ($array as &$n) {
// $n is a reference to an item in $array
$n = $n * 2;
}
// $array == [2, 4, 6, 8, 10]
// without an unset($n), $n is still a reference to the last item:
$array[4]
foreach ($array as $k => $n) {
// $array[4] will be updated with each value from $array...
echo "{$k} => {$n} ";
print_r($array);
}
// ...until ultimately the second-to-last value is copied onto the last
value
// output:
// 0 => 2 Array ( [0] => 2, [1] => 4, [2] => 6, [3] => 8, [4] => 2 )
// 1 => 4 Array ( [0] => 2, [1] => 4, [2] => 6, [3] => 8, [4] => 4 )
// 2 => 6 Array ( [0] => 2, [1] => 4, [2] => 6, [3] => 8, [4] => 6 )
// 3 => 8 Array ( [0] => 2, [1] => 4, [2] => 6, [3] => 8, [4] => 8 )
// 4 => 8 Array ( [0] => 2, [1] => 4, [2] => 6, [3] => 8, [4] => 8 )
?>
--
Edit bug report at https://bugs.php.net/bug.php?id=71458&edit=1
--
Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=71458&r=trysnapshot54
Try a snapshot (PHP 5.5): https://bugs.php.net/fix.php?id=71458&r=trysnapshot55
Try a snapshot (trunk): https://bugs.php.net/fix.php?id=71458&r=trysnapshottrunk
Fixed in SVN: https://bugs.php.net/fix.php?id=71458&r=fixed
Fixed in release: https://bugs.php.net/fix.php?id=71458&r=alreadyfixed
Need backtrace: https://bugs.php.net/fix.php?id=71458&r=needtrace
Need Reproduce Script: https://bugs.php.net/fix.php?id=71458&r=needscript
Try newer version: https://bugs.php.net/fix.php?id=71458&r=oldversion
Not developer issue: https://bugs.php.net/fix.php?id=71458&r=support
Expected behavior: https://bugs.php.net/fix.php?id=71458&r=notwrong
Not enough info: https://bugs.php.net/fix.php?id=71458&r=notenoughinfo
Submitted twice: https://bugs.php.net/fix.php?id=71458&r=submittedtwice
register_globals: https://bugs.php.net/fix.php?id=71458&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=71458&r=php4
Daylight Savings: https://bugs.php.net/fix.php?id=71458&r=dst
IIS Stability: https://bugs.php.net/fix.php?id=71458&r=isapi
Install GNU Sed: https://bugs.php.net/fix.php?id=71458&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=71458&r=float
No Zend Extensions: https://bugs.php.net/fix.php?id=71458&r=nozend
MySQL Configuration Error: https://bugs.php.net/fix.php?id=71458&r=mysqlcfg