Re: suggestion about ternary operator
| From: | Alban | Date: | Sat, 21 Nov 2009 05:46:39 +0000 |
| Subject: | Re: suggestion about ternary operator | ||
| References: | 1 2 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-46141@lists.php.net to get a copy of this message | ||
Le Fri, 20 Nov 2009 23:28:39 -0600, Larry Garfield a écrit :
> On Friday 20 November 2009 11:12:29 pm Alban wrote:
>
>> This is not a big problem but if a solution exists, this would be so
>> cool ! Especialy when we have to check existance of twenty or more key
>> in array. Code would be be lighter and clear.
>
> I cannot comment on the rest of your post right now, but if you are
> checking for the existence of a bunch of keys in an associative array
> and setting defaults if they are not set, the following will be
> considerably faster and easier to read:
>
> $my_array += array(
> 'a' => 'A',
> 'b' => 'B',
> 'c' => 'C',
> );
>
> That will set $my_array['a'] to A iff it doesn't exist, ['b'] to B iff
> it doesn't exist, etc. That is far nicer to read than a bunch of
> ternaries, short-circuited or no. You can even stick the defaults array
> into a function and call it from various places to ensure your array
> always has the same sane defaults.
Yes, union operator is a pretty solution for arrays.
--
Alban Leroux seza@paradoxal.org