php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #70635 an assignment operation modifies the source array
Submitted: 2015-10-04 11:16 UTC Modified: 2015-10-06 03:21 UTC
From: roger21 at free dot fr Assigned:
Status: Not a bug Package: Arrays related
PHP Version: 5.4.45 OS: linux debian 7.9 (old stable)
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: roger21 at free dot fr
New email:
PHP Version: OS:

 

 [2015-10-04 11:16 UTC] roger21 at free dot fr
Description:
------------
libapache2-mod-php5 5.4.45-0+deb7u1

php.ini not modified

(same bug with the cli)

the problem : the assignment operation in the foreach loop from one array to another modifies the source array (it shouldn't) - it does not if the source array is passed by reference in the foreach loop (it shouldn't either in this case but it could have)

the test script is long because the source array is long (147) but the code is simple and obvious

i don't know if the source array matters but i guess it does, otherwise the bug would have already been found

Test script:
---------------
http://roger21.free.fr/test/test_colors.txt

Expected result:
----------------
145 yellow
146 yellowgreen

145 yellow
146 yellowgreen

145 yellow
146 yellowgreen


Actual result:
--------------
145 yellow
146 yellowgreen

145 yellow
146 yellowgreen

145 yellow
146 yellow


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2015-10-05 18:42 UTC] requinix@php.net
-Status: Open +Status: Not a bug
 [2015-10-05 18:42 UTC] requinix@php.net
After the first foreach ends, $color will still be a reference to the last element in the $colors array. When the second foreach starts it will reuse $color, thus updating that last element each step through the loop.

Don't use a reference, or unset($color) after the first loop.
 [2015-10-06 03:21 UTC] roger21 at free dot fr
holy cow, that makes sense and that sucks (i mean, i suck at php)

thanks a lot
 
PHP Copyright © 2001-2019 The PHP Group
All rights reserved.
Last updated: Sat Dec 14 13:01:24 2019 UTC