3 ms·
Silent failures for indexing on primitives? $foo = 1; $foo[1]; // no problems $foo = true; $foo[0]; // seems good to php PHP's choose-your-own-error-adventur
by bpicolo 9y ago
Silent failures for indexing on primitives?
$foo = 1;
$foo[1]; // no problems
$foo = true;
$foo[0]; // seems good to php
PHP's choose-your-own-error-adventure with all errors on (which is the only good choice) is pretty silly. arrays become somewhat unwieldy because the core libs for arrays are deficient, missing things like a get_default($array, $key, $default).
The array core lib in general is troublesome to understand/intuit different pieces for, probably because mashing hash types and lists together leads to all sorts of ambiguity (like ambiguous json decoding/encoding). Want some subset of an array? Sure, that's the super-intuitive `array_intersect_key($array, array_flip($keys));`
The core array type is really a frustrating experience in PHP, and interop deficiencies with ArrayObject make it unusable as an alternative.
- stephenr 9y ago> get_default($array, $key, $default). $array[$key] ?? $default im still not sure what any of this has to do with "the only secure php is no php"?
- bpicolo 9y agoWith all notices on accessing an undefined index is a notice, which you're doing with that syntax, no?
- CiPHPerCoder 9y agoNope. https://3v4l.org/J45qU https://3v4l.org/J45qU
- bpicolo 9y agoAhh this is new in 7.0. Good to know, hadn't seen that. Still of the opinion that the array builtins are a hassle. =]
- deleted 9y ago[deleted]
- aruggirello 9y agoNo notices - But this syntax has been introduced in 7.0. Old style is: isset($array[$key]) ? $array[$key] : $default Which is, admittedly, just a bit uglier. Edit: isset() is a language construct, not a function call. So it does not incur performance penalties. The ?? operator was introduced for 'cosmetic' reasons only, so don't rush to update your source...