Re: suggestion about ternary operator
| From: | Larry Garfield | Date: | Sat, 21 Nov 2009 05:28:39 +0000 |
| Subject: | Re: suggestion about ternary operator | ||
| References: | 1 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-46140@lists.php.net to get a copy of this message | ||
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.
--
Larry Garfield
larry@garfieldtech.com