Re: Implicit isset/isempty check on short-ternary operator
| From: | Adam Richardson | Date: | Fri, 08 Apr 2011 07:02:07 +0000 |
| Subject: | Re: Implicit isset/isempty check on short-ternary operator | ||
| References: | 1 2 3 4 5 6 7 8 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-51834@lists.php.net to get a copy of this message | ||
>
>
>> We need to be careful about changing the beahviour of existing
> operators.
>
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';
Granted, the last example might be infrequent, but I think there's also a
value in keeping the form of the double ternary (if used at all) the same as
the standard ternary operator for consistency sake.
Adam
--
Nephtali: A simple, flexible, fast, and security-focused PHP framework
http://nephtaliproject.com