[PHP-BUG] Bug #71214 [NEW]: foreach() with inline assignment breaks references

[email protected] ("bugs dot php dot net at ss dot st dot tc")
Newsgroups php.bugs
Message-ID <[email protected]>
From:             bugs dot php dot net at ss dot st dot tc
Operating system: Linux, OSX
PHP version:      7.0.1
Package:          Scripting Engine problem
Bug Type:         Bug
Bug description:foreach() with inline assignment breaks references

Description:
------------
I believe both PHP 5.6 and 7 have bugs, but also they behave
differently.
Tested on PHP 5.6.12, 5.6.16, 7.0.0, 7.0.1.


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

# source array that we'll reference to later
$source = [ 'subarray' => ['item' => 'old value ---'] ];


# $ref1 is a reference to $source, assigned 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 a "normal" reference to $source
$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 done using a function
function getSource() {
	return [ 'subarray' => ['item' => 'old value ---'] ];
}


# $ref3 is a reference to a result of getSource(), assigned 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

-- 
Edit bug report at https://bugs.php.net/bug.php?id=71214&edit=1
-- 
Try a snapshot (PHP 5.4):   https://bugs.php.net/fix.php?id=71214&r=trysnapshot54
Try a snapshot (PHP 5.5):   https://bugs.php.net/fix.php?id=71214&r=trysnapshot55
Try a snapshot (trunk):     https://bugs.php.net/fix.php?id=71214&r=trysnapshottrunk
Fixed in SVN:               https://bugs.php.net/fix.php?id=71214&r=fixed
Fixed in release:           https://bugs.php.net/fix.php?id=71214&r=alreadyfixed
Need backtrace:             https://bugs.php.net/fix.php?id=71214&r=needtrace
Need Reproduce Script:      https://bugs.php.net/fix.php?id=71214&r=needscript
Try newer version:          https://bugs.php.net/fix.php?id=71214&r=oldversion
Not developer issue:        https://bugs.php.net/fix.php?id=71214&r=support
Expected behavior:          https://bugs.php.net/fix.php?id=71214&r=notwrong
Not enough info:            https://bugs.php.net/fix.php?id=71214&r=notenoughinfo
Submitted twice:            https://bugs.php.net/fix.php?id=71214&r=submittedtwice
register_globals:           https://bugs.php.net/fix.php?id=71214&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=71214&r=php4
Daylight Savings:           https://bugs.php.net/fix.php?id=71214&r=dst
IIS Stability:              https://bugs.php.net/fix.php?id=71214&r=isapi
Install GNU Sed:            https://bugs.php.net/fix.php?id=71214&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=71214&r=float
No Zend Extensions:         https://bugs.php.net/fix.php?id=71214&r=nozend
MySQL Configuration Error:  https://bugs.php.net/fix.php?id=71214&r=mysqlcfg
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.