|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #60024 Do not keep last element treated by foreach referenced
Submitted: 2011-10-09 21:09 UTC Modified: 2012-02-07 02:38 UTC
Avg. Score:4.0 ± 0.0
Reproduced:0 of 1 (0.0%)
From: chealer at gmail dot com Assigned:
Status: Duplicate Package: Scripting Engine problem
PHP Version: 5.3.8 OS:
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:
Solve the problem:
24 - 20 = ?
Subscribe to this entry?

 [2011-10-09 21:09 UTC] chealer at gmail dot com
As explained on :


Reference of a $value and the last array element remain even after the foreach loop. It is recommended to destroy it by unset().

In my opinion, PHP shouldn't keep the last element referenced by default, but at least, please provide a syntax which will not keep it. The current situations causes bugs like


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2012-02-06 00:47 UTC] DeveloperGuy2008 at yahoo dot com
Please fix this. It creates a lot of hard to debug bugs.
 [2012-02-06 00:53 UTC]
-Status: Open +Status: Duplicate
 [2012-02-06 00:53 UTC]
This has previously been discussed and rejected, due to users relying both on 
PHP's lack of block scope and foreach's by-reference syntax. (Sometimes both at 
once, unfortunately.)

Duplicate of bug #29992, bug #47388, bug #54189, bug #50485, and probably a bunch 
of others.
 [2012-02-06 22:24 UTC] chealer at gmail dot com
Could the discussion be linked to?

This is not a duplicate of any of the reports indicated. The reports indicated are reports of application bugs as PHP bugs, which are therefore invalid. This report is asking to change PHP, in order to avoid such application bugs.
 [2012-02-07 00:10 UTC]
Sure, it's come up on the mailing list a few times, but the one I was thinking of 
specifically is:
 [2012-02-07 02:38 UTC] chealer at gmail dot com
Thank you. I read the discussion once, but I didn't see where it rejects this. Could the rejections be quoted or pointed more specifically?
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Mon Jun 17 11:01:31 2024 UTC