Re: Revisiting RFC: Engin e Warnings -- Undefined array index
| From: | Rowan Tommins | Date: | Mon, 12 Dec 2022 22:58:38 +0000 |
| Subject: | Re: Revisiting RFC: Engin e Warnings -- Undefined array index | ||
| References: | 1 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-119111@lists.php.net to get a copy of this message | ||
On 12 December 2022 22:20:27 GMT, Dan Liebner <dliebner@gmail.com> wrote:
>Here's my biggest criticism: the change essentially forces this:
>$arr['key']
>
>to be rewritten as this (or some other intolerably bad alternative):
>isset($arr) && isset($arr['key']) && $arr['key']
Fortunately, this is not the case, the actual replacement code is this:
$arr['key'] ?? null
There has previously been a suggestion of introducing an even shorter syntax, such as this:
$arr[?'key']
(With the difference that only one key is marked optional, whereas ?? allows any number of
dimensions - $foo['bar']['baz'] ?? null would be equivalent to
$foo[?'bar'][?'baz'].)
This would avoid the inconsistency of configurable behaviour, and allow users to make clear with a
single keystroke that they know a key might be missing, while giving them useful feedback for typos
and other mistakes that lead to a key being unexpectedly missing.
I think this is a general approach that should be more explicitly embraced: introduce convenient
syntax for valid use cases, while making accidents obvious.
Regards,
Hi Dan,
--
Rowan Tommins
[IMSoP]