Doc #70594 [Opn]: behviour of unset inside foreach is undocumented

From: Date: Sat, 28 Jan 2017 17:36:52 +0000
Subject: Doc #70594 [Opn]: behviour of unset inside foreach is undocumented
References: 1  Groups: php.doc.bugs 
Request: Send a blank email to doc-bugs+get-14395@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=70594&edit=1 ID: 70594 Updated by: cmb@php.net Reported by: lucas at threeam dot com dot au Summary: behviour of unset inside foreach is undocumented Status: Open Type: Documentation Problem Package: Documentation problem PHP Version: Irrelevant Block user comment: N Private report: N New Comment: In my opinion, the foreach man page should document whats happening if elements/properties are added or removed during the iteration. Previous Comments: ------------------------------------------------------------------------ [2015-09-28 01:14:00] lucas at threeam dot com dot au Description: ------------ --- From manual page: http://www.php.net/function.unset --- Does not explicitly mention whether a construct like // Now delete every item, but leave the array itself intact: foreach ($array as $i => $value) { unset($array[$i]); } is safe, even though it's implied since that example comes from a (non-user-contributed) note in http://www.php.net/language.types.array 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])) { unset($a[$i]); } } but this is okay: for ($i = 0, $n = count($a); $i < $n; $i++) { if (!sometest($a[$i])) { unset($a[$i]); } } ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=70594&edit=1

« previous php.doc.bugs (#14395) next »