In that case, maybe reuse something similar to the null-safe operator ?
Something like `$foo['bar']?`
With the ability to have some freedom in placing the `?` character.
This way, you have both syntactic sugar:
return $foo['one']?;
// is roughly equivalent to:
return $foo['one'] ?? null;
return $foo['one']?['two'];
// is roughly equivalent to:
return $foo['one']['two'] ?? null;
And here it gets interesting:
return $foo['one']['two']?['three'];
// is roughly equivalent to:
if (!isset($foo['bar']) {
trigger_error('Undefined key "one" in ...', ...);
}
return $foo['one']['two']['three'] ?? null;
This brings a "new" way to deal with nested array keys (like explained
in the first code samples).
I don't know about the parser yet, but technically, in language
semantics, it is a unary-right, contrary to the "?" operator which is
binary left&right and must be used alongside the ":" operator.
Would need an implementation and some tests to ensure the reliability.
Le 27/03/2026 à 06:26, Dan Liebner a écrit :
On 2026-03-27 03:58, Rowan Tommins [IMSoP] wrote:
> On 26 March 2026 07:51:31 GMT, Rob Landers <[email protected]>
wrote:
>
> - an "optional key access" operator like $foo[?'bar'] that makes
the intent explicit but concise
So, syntactic sugar for "($foo['bar'] ?? null)".
That, to me, would be a massive improvement in terms of code readability.
On Thu, Mar 26, 2026 at 11:40 PM Morgan <[email protected]> wrote:
On 2026-03-27 03:58, Rowan Tommins [IMSoP] wrote:
> On 26 March 2026 07:51:31 GMT, Rob Landers <[email protected]>
wrote:
>
> - an "optional key access" operator like $foo[?'bar'] that makes
the intent explicit but concise
So, syntactic sugar for "($foo['bar'] ?? null)".