php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #71216 foreach() with inline assignment doesn't keep references
Submitted: 2015-12-24 20:04 UTC Modified: 2015-12-24 21:40 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: bugs dot php dot net at ss dot st dot tc Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 7.0.1 OS: Linux, OSX
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: bugs dot php dot net at ss dot st dot tc
New email:
PHP Version: OS:

 

 [2015-12-24 20:04 UTC] bugs dot php dot net at ss dot st dot tc
Description:
------------
When an inline assignment of array_expression is done right in foreach() statement, all further references to iterated array items seem to become their copies.

I believe both PHP 5.6 and 7 are affected, yet still they behave differently.
Tested on PHP 5.6.12, 5.6.16, 7.0.0, 7.0.1.


Test script:
---------------
<?php

# source array
$source = [ 'subarray' => ['item' => 'old value ---'] ];


# $ref1 is assigned to $source right in the foreach()
foreach ( $ref1 = $source as &$subarray ) {
	$subarray['item'] = 'NEW value';
	echo $subarray['item'], PHP_EOL;
	echo $ref1['subarray']['item'], PHP_EOL;
}
echo PHP_EOL;


# $ref2 is assigned to $source normally, as a separate operation
$ref2 = $source;
foreach ( $ref2 as &$subarray ) {
	$subarray['item'] = 'NEW value';
	echo $subarray['item'], PHP_EOL;
	echo $ref2['subarray']['item'], PHP_EOL;
}
echo PHP_EOL;


########################################

# similar to $source array, but implemented using a function
function getSource() {
	return [ 'subarray' => ['item' => 'old value ---'] ];
}


# $ref3 is assigned to result of getSource() right in the foreach()
foreach ( $ref3 = getSource() as &$subarray ) {
	$subarray['item'] = 'NEW value';
	echo $subarray['item'], PHP_EOL;
	echo $ref3['subarray']['item'], PHP_EOL;
}
echo PHP_EOL;


# $ref4 is a "normal" reference to a result of getSource()
$ref4 = getSource();
foreach ( $ref4 as &$subarray ) {
	$subarray['item'] = 'NEW value';
	echo $subarray['item'], PHP_EOL;
	echo $ref3['subarray']['item'], PHP_EOL;
}

Expected result:
----------------
both PHP 5 and 7:

NEW value
NEW value

NEW value
NEW value

NEW value
NEW value

NEW value
NEW value

Actual result:
--------------
PHP 7: 

NEW value
old value ---

NEW value
NEW value

NEW value
old value ---

NEW value
old value ---


PHP 5:

NEW value
old value ---

NEW value
NEW value

NEW value
NEW value

NEW value
NEW value

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2015-12-24 20:08 UTC] bugs dot php dot net at ss dot st dot tc
(typo in test script, closing, I really wish I can edit it after it's been submitted..)
 [2015-12-24 21:40 UTC] requinix@php.net
-Status: Open +Status: Not a bug
 [2015-12-24 21:40 UTC] requinix@php.net
.
 
PHP Copyright © 2001-2026 The PHP Group
All rights reserved.
Last updated: Wed Oct 07 14:00:01 2026 UTC