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
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: basil dot hussain at kodakweddings dot com
New email:
PHP Version: OS:

 

 [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 23:01:28 2024 UTC