Re: [RFC] Implicit isset() in Shorthand Ternary Operator
| From: | Andrea Faulds | Date: | Thu, 18 Sep 2014 11:41:52 +0000 |
| Subject: | Re: [RFC] Implicit isset() in Shorthand Ternary Operator | ||
| References: | 1 2 3 4 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-77307@lists.php.net to get a copy of this message | ||
On 18 Sep 2014, at 12:32, Pete Boere <pete@the-echoplex.net> wrote:
> I'm seeing '??' as analogous to the way JS developers use '||', and I
> use
> that all the time when writing JS.
PHP already has a direct equivalent to ||, namely the ?: operator. However, unfortunately PHP always
spits out a notice on non-existent indices, unlike JS which just results in undefined, so we need to
add a special operator to silence it.
> Personally I wouldn't be interested in a function version because the
> short-circuiting of '??' is an important distinction; not something you can
> replicate with a function. Therefore having both would be confusing IMO.
We could use a function *syntax*, though, but I don’t like this idea. coalesce() and ifsetor() are
both ugly to me, and it’s not super obvious what they do:
var_dump(coalesce($_GET[‘foobar’], 3));
vs.
var_dump($_GET[‘foobar’] ?? 3);
?? also has the advantage of being shorter.
> Also, not much sure about a '??=', perhaps it should be a followup RFC
> should '??' be accepted.
It’d make sense to do it within this RFC if possible. If it seems too controversial, it could
always have a separate vote within the RFC.
--
Andrea Faulds
http://ajf.me/