|  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
 [2015-10-04 11:16 UTC] roger21 at free dot fr
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:

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


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2015-10-05 18:42 UTC]
-Status: Open +Status: Not a bug
 [2015-10-05 18:42 UTC]
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-2021 The PHP Group
All rights reserved.
Last updated: Thu Sep 16 12:03:37 2021 UTC