php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #71458 foreach docs should expand upon the references warning
Submitted: 2016-01-26 19:51 UTC Modified: 2016-05-31 17:00 UTC
Votes:1
Avg. Score:4.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: requinix@php.net Assigned: cmb (profile)
Status: Closed Package: Scripting Engine problem
PHP Version: Irrelevant OS:
Private report: No CVE-ID: None
 [2016-01-26 19:51 UTC] requinix@php.net
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 )

?>


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2016-01-28 12:43 UTC] bwoebi@php.net
Another option though would be to change behavior (in next major) and deref foreach () references at the end of the loop...
 [2016-05-31 08:01 UTC] cronfy at gmail dot com
> Another option though would be to change behavior (in next major) and deref foreach () references at the end of the loop...

This can break existing code - reference to last array item may be expected.

But is it possible to deref foreach $value at the START of foreach? I think it should work fine.
 [2016-05-31 16:55 UTC] cmb@php.net
Automatic comment from SVN on behalf of cmb
Revision: http://svn.php.net/viewvc/?view=revision&amp;revision=339246
Log: fix #71458: foreach docs should expand upon the references warning
 [2016-05-31 17:00 UTC] cmb@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: cmb
 [2016-05-31 17:00 UTC] cmb@php.net
In my opinion, neither automatically deferencing the loop variable
before nor after the loop would fit to PHP's *function* scope of
local variables. However, feel free to file a (separate) feature
request.
 [2020-02-07 06:07 UTC] phpdocbot@php.net
Automatic comment on behalf of cmb
Revision: http://git.php.net/?p=doc/en.git;a=commit;h=1d7caa1a6098afe39ec60c2304df03d2a1a03c77
Log: fix #71458: foreach docs should expand upon the references warning
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Mar 29 09:01:28 2024 UTC