From: eric dot prevoteau at ac-strasbourg dot fr
Operating system: Linux
PHP version: 5.5.18
Package: Arrays related
Bug Type: Bug
Bug description:empty foreach modifies array content
Description:
------------
This bug occurs at least on PHP 5.5.9, 5.5.16 & 5.6.0 on both gentoo and
ubuntu 14.04.
When 2 foreach iterate over the same array with the same variable to
store the iteration value and the 1st foreach uses reference on the
iteration variable but not the 2nd foreach, at the end of the 2nd
foreach, the last value of the array is modified.
the problem occurs:
* when array values are objects or integers
* only if the 1st foreach uses '&' but not the 2nd foreach
* only if the 2 foreach use the same variable to store the iteration
value
* on both apache2 module and cli
adding print_r inside the 2nd foreach shows array changes. At each
iteration, the last value of the array is modified.
My configuration for PHP 5.6.0 (cli) is the following:
'./configure' '--prefix=/usr' '--build=x86_64-pc-linux-gnu'
'--host=x86_64-pc-linux-gnu' '--mandir=/usr/share/man'
'--infodir=/usr/share/info' '--datadir=/usr/share' '--sysconfdir=/etc'
'--localstatedir=/var/lib' '--prefix=/usr/lib64/php5.6'
'--mandir=/usr/lib64/php5.6/man' '--infodir=/usr/lib64/php5.6/info'
'--libdir=/usr/lib64/php5.6/lib' '--with-libdir=lib64'
'--without-pear'
'--enable-maintainer-zts' '--disable-bcmath' '--without-bz2'
'--disable-calendar' '--enable-ctype' '--with-curl=/usr'
'--enable-dom'
'--without-enchant' '--disable-exif' '--enable-fileinfo'
'--enable-filter' '--disable-ftp' '--with-gettext=/usr'
'--without-gmp'
'--enable-hash' '--without-mhash' '--with-iconv'
'--disable-intl'
'--disable-ipv6' '--enable-json' '--without-kerberos'
'--enable-libxml'
'--with-libxml-dir=/usr' '--enable-mbstring' '--with-mcrypt=/usr'
'--without-mssql' '--with-onig=/usr' '--without-openssl'
'--without-openssl-dir' '--enable-pcntl' '--disable-phar'
'--enable-pdo'
'--disable-opcache' '--without-pgsql' '--enable-posix'
'--without-pspell' '--without-recode' '--enable-simplexml'
'--disable-shmop' '--without-snmp' '--disable-soap'
'--enable-sockets'
'--without-sqlite3' '--without-sybase-ct' '--disable-sysvmsg'
'--disable-sysvsem' '--disable-sysvshm' '--without-fpm-systemd'
'--without-tidy' '--enable-tokenizer' '--disable-wddx'
'--enable-xml'
'--disable-xmlreader' '--enable-xmlwriter' '--without-xmlrpc'
'--without-xsl' '--enable-zip' '--with-zlib=/usr'
'--disable-debug'
'--without-cdb' '--without-db4' '--disable-flatfile'
'--without-gdbm'
'--disable-inifile' '--without-qdbm' '--with-freetype-dir=/usr'
'--with-t1lib=/usr' '--disable-gd-jis-conv' '--with-jpeg-dir=/usr'
'--with-png-dir=/usr' '--without-xpm-dir' '--without-vpx-dir'
'--with-gd' '--with-ldap=/usr' '--without-ldap-sasl'
'--with-mysql=mysqlnd' '--with-mysqli=mysqlnd'
'--with-mysql-sock=/var/run/mysqld/mysqld.sock' '--without-pdo-dblib'
'--with-pdo-mysql=mysqlnd' '--without-pdo-pgsql'
'--without-pdo-sqlite'
'--without-pdo-odbc' '--without-readline' '--without-libedit'
'--without-mm' '--with-pic' '--with-pcre-regex=/usr'
'--with-pcre-dir=/usr' '--with-config-file-path=/etc/php/cli-php5.6'
'--with-config-file-scan-dir=/etc/php/cli-php5.6/ext-active'
'--disable-embed' '--enable-cli' '--disable-cgi'
'--disable-fpm'
'--without-apxs2' 'build_alias=x86_64-pc-linux-gnu'
'host_alias=x86_64-pc-linux-gnu' 'CFLAGS=-march=native '-O2'
'-pipe''
'LDFLAGS=-Wl,-O1 '-Wl,--as-needed'' 'CPPFLAGS='
'CXXFLAGS=-march=native
'-O2' '-pipe''
No loaded modules.
No changes made on php.ini
Test script:
---------------
$a = array();
$a[1] = 1;
$a[2] = 3;
$a[3] = 5;
$a[4] = 7;
foreach($a as &$v)
{
}
foreach($a as $v)
{
}
print_r($a);
Expected result:
----------------
Array
(
[1] => 1
[2] => 3
[3] => 5
[4] => 7
)
Actual result:
--------------
Array
(
[1] => 1
[2] => 3
[3] => 5
[4] => 5
)
--
Edit bug report at https://bugs.php.net/bug.php?id=68299&edit=1
--
Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=68299&r=trysnapshot54
Try a snapshot (PHP 5.5): https://bugs.php.net/fix.php?id=68299&r=trysnapshot55
Try a snapshot (trunk): https://bugs.php.net/fix.php?id=68299&r=trysnapshottrunk
Fixed in SVN: https://bugs.php.net/fix.php?id=68299&r=fixed
Fixed in release: https://bugs.php.net/fix.php?id=68299&r=alreadyfixed
Need backtrace: https://bugs.php.net/fix.php?id=68299&r=needtrace
Need Reproduce Script: https://bugs.php.net/fix.php?id=68299&r=needscript
Try newer version: https://bugs.php.net/fix.php?id=68299&r=oldversion
Not developer issue: https://bugs.php.net/fix.php?id=68299&r=support
Expected behavior: https://bugs.php.net/fix.php?id=68299&r=notwrong
Not enough info: https://bugs.php.net/fix.php?id=68299&r=notenoughinfo
Submitted twice: https://bugs.php.net/fix.php?id=68299&r=submittedtwice
register_globals: https://bugs.php.net/fix.php?id=68299&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=68299&r=php4
Daylight Savings: https://bugs.php.net/fix.php?id=68299&r=dst
IIS Stability: https://bugs.php.net/fix.php?id=68299&r=isapi
Install GNU Sed: https://bugs.php.net/fix.php?id=68299&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=68299&r=float
No Zend Extensions: https://bugs.php.net/fix.php?id=68299&r=nozend
MySQL Configuration Error: https://bugs.php.net/fix.php?id=68299&r=mysqlcfg