|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[2014-07-16 16:35 UTC] nikic@php.net
-Assigned To:
+Assigned To: nikic
[2014-09-20 19:59 UTC] nikic@php.net
[2014-09-20 19:59 UTC] nikic@php.net
-Status: Assigned
+Status: Closed
[2014-09-20 20:00 UTC] nikic@php.net
[2014-09-20 20:00 UTC] nikic@php.net
[2014-09-22 08:28 UTC] ab@php.net
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sun Nov 09 04:00:01 2025 UTC |
Description: ------------ When passing an array by value to a function, which immediately returns that array, and then using that return value in a foreach with a reference, it allows modifying the original array as if it was passed by reference. These variants work properly: ............. Variant 1 [same main body] function z($t) { // Force copy-on-write $t[0] .= 'x'; return $t; } ............. ............. Variant 2 $a = [ 'a', 'b', 'c' ]; $b = z($a); // Assigning to variable causes proper copy-on-write behavior foreach($b as &$x) { $x .= 'q'; } print_r($a); [same z function] ............. Test script: --------------- <?php $a = [ 'a', 'b', 'c' ]; foreach(z($a) as &$x) { $x .= 'q'; } print_r($a); function z($t) { return $t; } Expected result: ---------------- Array ( [0] => a [1] => b [2] => c ) Actual result: -------------- Array ( [0] => aq [1] => bq [2] => cq )