Doc #70594 [NEW]: behviour of unset inside foreach is undocumented
| From: | lucas at threeam dot com dot au | Date: | Mon, 28 Sep 2015 01:14:02 +0000 |
| Subject: | Doc #70594 [NEW]: behviour of unset inside foreach is undocumented | ||
| Groups: | php.doc.bugs | ||
| Request: | Send a blank email to doc-bugs+get-12782@lists.php.net to get a copy of this message | ||
From: lucas at threeam dot com dot au
Operating system:
PHP version: Irrelevant
Package: Documentation problem
Bug Type: Documentation Problem
Bug description:behviour of unset inside foreach is undocumented
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 bug report at https://bugs.php.net/bug.php?id=70594&edit=1
--
Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=70594&r=trysnapshot54
Try a snapshot (PHP 5.5): https://bugs.php.net/fix.php?id=70594&r=trysnapshot55
Try a snapshot (trunk): https://bugs.php.net/fix.php?id=70594&r=trysnapshottrunk
Fixed in SVN: https://bugs.php.net/fix.php?id=70594&r=fixed
Fixed in release: https://bugs.php.net/fix.php?id=70594&r=alreadyfixed
Need backtrace: https://bugs.php.net/fix.php?id=70594&r=needtrace
Need Reproduce Script: https://bugs.php.net/fix.php?id=70594&r=needscript
Try newer version: https://bugs.php.net/fix.php?id=70594&r=oldversion
Not developer issue: https://bugs.php.net/fix.php?id=70594&r=support
Expected behavior: https://bugs.php.net/fix.php?id=70594&r=notwrong
Not enough info: https://bugs.php.net/fix.php?id=70594&r=notenoughinfo
Submitted twice: https://bugs.php.net/fix.php?id=70594&r=submittedtwice
register_globals: https://bugs.php.net/fix.php?id=70594&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=70594&r=php4
Daylight Savings: https://bugs.php.net/fix.php?id=70594&r=dst
IIS Stability: https://bugs.php.net/fix.php?id=70594&r=isapi
Install GNU Sed: https://bugs.php.net/fix.php?id=70594&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=70594&r=float
No Zend Extensions: https://bugs.php.net/fix.php?id=70594&r=nozend
MySQL Configuration Error: https://bugs.php.net/fix.php?id=70594&r=mysqlcfg