RE: [PHP-DEV] Implicit isset/isempty check on short-ternary operator
| From: | Ford, Mike | Date: | Fri, 08 Apr 2011 09:06:52 +0000 |
| Subject: | RE: [PHP-DEV] Implicit isset/isempty check on short-ternary operator | ||
| References: | 1 2 3 4 5 6 7 8 9 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-51836@lists.php.net to get a copy of this message | ||
> -----Original Message-----
> From: Adam Richardson [mailto:simpleshot@gmail.com]
> Sent: 08 April 2011 08:02
>
> Indeed.
>
> The '?' character already is special, so using '??' seems like a
> safe,
> practical approach. However, I'd prefer maintaining the form of the
> standard
> ternary operator with the colon ($value = $var['bar'] ?? : 'Bar was
> not
> set'; // value ="Bar was not) so the '??' operator could be applied
> in any
> situation that one would normally use the standard ternary operator.
>
> // standard
> $value = isset($a[$key]) ? $a[$key] : 'Not set';
>
> // new ?? double ternary that performs isset check and omits second
> expression
> $value = $a[$key] ?? : 'Not set';
>
> // new ?? double ternary that performs isset check and uses second
> expression
> $value = $a[$key] ?? strtoupper($a[$key]) : 'Not set';
>
Like it! I would have proposed ??: for the isset() variant of ?:,
and I think decomposing it into a full ternary operator in this way,
with ??: as a degenerate variant, is brilliant.
(Only downside is, people would have to stop referring to *the*
ternary operator!)
Cheers!
Mike
--
Mike Ford,
Electronic Information Developer, Libraries and Learning Innovation,
Leeds Metropolitan University, C507 City Campus,
Woodhouse Lane, LEEDS, LS1 3HE, United Kingdom
Email: m.ford@leedsmet.ac.uk
Tel: +44 113 812 4730
To view the terms under which this email is distributed, please go to http://disclaimer.leedsmet.ac.uk/email.htm