|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #70594 behviour of unset inside foreach is undocumented
Submitted: 2015-09-28 01:14 UTC Modified: 2017-01-28 17:36 UTC
From: lucas at threeam dot com dot au Assigned:
Status: Open Package: Documentation problem
PHP Version: Irrelevant OS:
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
Block user comment
Status: Assign to:
Bug Type:
From: lucas at threeam dot com dot au
New email:
PHP Version: OS:


 [2015-09-28 01:14 UTC] lucas at threeam dot com dot au
From manual page:

Does not explicitly mention whether a construct like

// Now delete every item, but leave the array itself intact:
foreach ($array as $i => $value) {

is safe, even though it's implied since that example comes from a (non-user-contributed) note in

You should probably also mention that this does not hold for 'for' loops unless the size of array is captured first. e.g. this is bad:

for ($i = 0; $i < count($a); $i++) {
    if (!sometest($a[$i])) {

but this is okay:

for ($i = 0, $n = count($a); $i < $n; $i++) {
    if (!sometest($a[$i])) {


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2017-01-28 17:36 UTC]
In my opinion, the `foreach` man page should document whats
happening if elements/properties are added or removed
during the iteration.
PHP Copyright © 2001-2023 The PHP Group
All rights reserved.
Last updated: Sat Dec 02 13:01:27 2023 UTC