|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #41169 foreach() on empty array causes key to be created (Works with PHP 5!)
Submitted: 2007-04-23 04:36 UTC Modified: 2007-12-08 11:54 UTC
Avg. Score:3.8 ± 1.2
Reproduced:6 of 8 (75.0%)
Same Version:2 (33.3%)
Same OS:2 (33.3%)
From: mobile-g at softhome dot net Assigned:
Status: Wont fix Package: Scripting Engine problem
PHP Version: 4.4.6 OS: Red Hat
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.
Block user comment
Status: Assign to:
Bug Type:
From: mobile-g at softhome dot net
New email:
PHP Version: OS:


 [2007-04-23 04:36 UTC] mobile-g at softhome dot net
running a foreach loop on an empty array causes the test key to be created.

Note this code has been pulled from a class, I have kept this structure in case that has relevance.

Reproduce code:
var $myArray = Array();

foreach($this -> mayArray['value'] as $value){

    $myArray += $value;

Expected result:
Ideally the loop wouldn't run, or would error when trying to locate a none existant key.

Actual result:
myArray() has an empty key called "value" added to it.


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2007-04-23 04:38 UTC] mobile-g at softhome dot net
var $myArray = Array();

foreach($this -> mayArray['value'] as $value){
    $myVAR += $value; //changed this line

actually it isn't asssigning to itself...
this version is correct.
 [2007-04-23 05:03 UTC] judas dot iscariote at gmail dot com

class foo {

var $myArray = Array();

    function foo() {

        foreach($this->myArray['value'] as $value){


$foo = new Foo();

produces the behaviuor you describe with PHP4

array(1) {

PHP5 works as expected.

My suggestion for the reporter is to upgrade to PHP5 ASAP, you shouldnt be using PHP4 nowdays anyway.
 [2007-04-23 09:20 UTC]
Assigned to the maintainer.
PHP Copyright © 2001-2022 The PHP Group
All rights reserved.
Last updated: Thu Aug 11 18:03:36 2022 UTC