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
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:
50 + 6 = ?
Subscribe to this entry?

 
 [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-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 19 08:01:28 2024 UTC