Re: [PHP-DEV] Proposal for RFC to remove undefined array index warning when used ina ternary

2022-02-27 Thread Mel Dafert
>Hi,
>
>In PHP, you can write `$array['a']['b']['c'] ?? 'default';`. It works when 
>either null or inexistant index is encountered at any depth of the expression.
>
>—Claude

You're right, I did not know that. Thank you.
That basically replaces the need for my proposal, then.

Regards,
Mel

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: https://www.php.net/unsub.php



Re: [PHP-DEV] Proposal for RFC to remove undefined array index warning when used ina ternary

2022-02-27 Thread Claude Pache


> Le 27 févr. 2022 à 18:06, Robert Landers  a écrit :
> 
>> $val = $array[?'a'][?'b'][?'c'] ?? 'default';
>> 
>> Currently, the last example would need to be a rather unwieldy ternary:
>> 
>> $val = (isset($array['a']) && isset($array['a']['b']) &&
>> isset($array['a']['b']['c']) ? $array['a']['b']['c'] : 'default';
>> 
> 
> I've been writing it like this:
> 
> $val = (($array['a'] ?? [])['b'] ?? [])['c'] ?? 'default';
> 
> which is still unwieldy. I don't actually know if it is required to use []
> vs. null, but it makes sense to use an empty array to me.
> 


Hi,

In PHP, you can write `$array['a']['b']['c'] ?? 'default';`. It works when 
either null or inexistant index is encountered at any depth of the expression.

—Claude