php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #14152 foreach() can create objects with blank property names
Submitted: 2001-11-20 13:22 UTC Modified: 2002-01-15 07:19 UTC
From: basil dot hussain at kodakweddings dot com Assigned:
Status: Not a bug Package: Class/Object related
PHP Version: 4.0.5 OS: Linux 2.2.16-enterprise
Private report: No CVE-ID: None
 [2001-11-20 13:22 UTC] basil dot hussain at kodakweddings dot com
Using the following code (which someone may do accidentally):

$myarray = array("X" => "A", "Y" => "B", "Z" => "C");
foreach($myarray as $key->$value) {
  ...
}

causes PHP (on each iteration) to create an object $key with the value of the current array element in a property with a blank name (because $value has just been created, and is therefore empty).

The following var_dump() output illustrates:

object(stdClass)(1) {
  [""]=>
  string(1) "A"
}

The creation of objects with blank property names seems like it shouldn't be possible (even unintentionally)!

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2001-11-22 18:45 UTC] edink@php.net
Much easier to reproduce by:

$a->$b="c";

Foreach loop should of course read

foreach($myarray as $key=>$value)

 [2002-01-15 07:19 UTC] lobbin@php.net
Bogus, user error.
 
PHP Copyright © 2001-2019 The PHP Group
All rights reserved.
Last updated: Mon Nov 18 21:01:37 2019 UTC