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
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
MUST BE VALID
Solve the problem:
49 - 38 = ?
Subscribe to this entry?

 
 [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-2024 The PHP Group
All rights reserved.
Last updated: Thu Apr 25 02:01:30 2024 UTC