Re: Undefined variables and the array append operator
| From: | Guilliam Xavier | Date: | Wed, 30 Mar 2022 14:32:39 +0000 |
| Subject: | Re: Undefined variables and the array append operator | ||
| References: | 1 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-117459@lists.php.net to get a copy of this message | ||
Hi Rowan,
Not really a "compelling reason why we should keep this inconsistency", but
I have occasionally relied on array autovivification *for sub-dimensions*,
e.g.:
```
function f(iterable $xs) {
$map = []; // initialization!
foreach ($xs as $x) {
// $map[foo($x)] ??= []; not needed
$map[foo($x)][] = bar($x); // autovivification
}
// Then e.g.:
foreach ($map as $foo => $bars) {
foreach ($bars as $bar) {
/* ... */
}
}
}
```
(adapted from my https://externals.io/message/114595#114611
message in the
"Disable autovivification on false" thread).
On the other hand, I agree that
$undefined[] = $x looks like a bug... are
both cases the exact same opcode? (if yes, I wouldn't really mind updating
my code, especially for consistency with other "append" operators like .=
or +=, and that could even be an opportunity to rewrite it in a more
"functional" style...)
Regards,
--
Guilliam Xavier