|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #9722 foreach() operates on a copy of an object
Submitted: 2001-03-13 05:09 UTC Modified: 2001-03-13 22:13 UTC
From: johanp at aditus dot nu Assigned:
Status: Closed Package: Class/Object related
PHP Version: 4.0.4pl1 OS: Linux 2.2.16
Private report: No CVE-ID: None
 [2001-03-13 05:09 UTC] johanp at aditus dot nu
The following two array looping statements are not equal
(Assuming Update() updates some instance variables in the 

foreach( $objs as $o )


for($i=0; $i<count($objs); ++$i)

In the first case the Update() method will change an anonymous copy of the object in the array while in the second case the update will work on the object in the array. Hence any changes that Update() might do will in the
first case not be done for the objects in the array.

Not sure if this is a genuine bug or just a consequence of the copying semantics for objects in PHP.


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2001-03-13 22:13 UTC]
Please read the manual:

"Note: Also note that foreach operates on a copy of the specified array, not the array itself, therefore the array pointer is not modified like with the each construct."


PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Jul 25 01:01:30 2024 UTC