Bug #68299 [Opn->Nab]: empty foreach modifies array content

From: Date: Fri, 24 Oct 2014 17:51:17 +0000
Subject: Bug #68299 [Opn->Nab]: empty foreach modifies array content
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-188294@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=68299&edit=1 ID: 68299 Updated by: requinix@php.net Reported by: eric dot prevoteau at ac-strasbourg dot fr Summary: empty foreach modifies array content -Status: Open +Status: Not a bug Type: Bug Package: Arrays related Operating System: Linux PHP Version: 5.5.18 Block user comment: N Private report: N New Comment: After the first foreach ends $v will still be a reference (to the last element in the array). When the second foreach starts updating $v with values it will also update the last element in the array; if you dump $a in the loop you'll see it change like [1, 3, 5, 1] [1, 3, 5, 3] [1, 3, 5, 5] [1, 3, 5, 5] Either don't use references or unset($v) after the first loop. Previous Comments: ------------------------------------------------------------------------ [2014-10-24 12:19:51] eric dot prevoteau at ac-strasbourg dot fr 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' '--wi! thout-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 this bug report at https://bugs.php.net/bug.php?id=68299&edit=1

« previous php.bugs (#188294) next »