Re: PHP7 Coalesce operator
| From: | shadda | Date: | Mon, 23 Nov 2015 21:02:16 +0000 |
| Subject: | Re: PHP7 Coalesce operator | ||
| References: | 1 2 3 4 5 6 7 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-89351@lists.php.net to get a copy of this message | ||
That’s not a bad idea, but it’s still limited in certain cases, where your list of possibles is,
lets say, complex.
At this point, I think you guys get what I’m after, and I’m wondering if anyone else finds the
idea as useful I as do.
In fact, if it didn’t break the One Law of PHP (thou shalt not break BC), I might even suggest
just giving ?: the same super power as ??, in not throwing a notice.
> On Nov 23, 2015, at 12:52 PM, Ryan Pallas <derokorian@gmail.com> wrote:
>
> Well use null coalesce + elvis together then?
>
> // Coalesce possible null values, and use truthy comparison in elvis to "coalesce" to
> a default
> $var = ($_GET['possible_value'] ?? $_COOKIE['secondary'] ?? false) ?:
> DEFAULTS['var'];
>
> On Mon, Nov 23, 2015 at 1:16 PM, shadda <shadda@gmail.com
> <mailto:shadda@gmail.com>> wrote:
> It’s not hate, I happen to like that ?? was added. I’m not raising a concern nor asking for
> a change in its behavior. Rather, I’m asking for another, similar operator that’s based on
> truth-evaluation (with PHP’s tender, loving conversion rules baked in), in *addition* to ?? which
> tests for null/undefined exclusively.
>
>
> > On Nov 23, 2015, at 12:08 PM, Phil Sturgeon <pjsturgeon@gmail.com
> > <mailto:pjsturgeon@gmail.com>> wrote:
> >
> > On Mon, Nov 23, 2015 at 9:52 AM, shadda <shadda@gmail.com
> > <mailto:shadda@gmail.com>> wrote:
> >> Er, that should read, “anything non (null, false, undefined) is true”
> >>
> >>> On Nov 23, 2015, at 11:51 AM, shadda <shadda@gmail.com
> >>> <mailto:shadda@gmail.com>> wrote:
> >>>
> >>> Hey, thanks for responding.
> >>>
> >>> However, I still think that misses the point, or at least the true utility of what
> >>> I’m proposing.
> >>>
> >>> In practice, you can ignore E_NOTICE and php will happily treat null and !isset()
> >>> as the same thing in *most* cases, so that’s not a good indicator of truthiness.
> >>>
> >>> Basically in ECMA, anything non-null, false, or undefined is true. PHP is very
> >>> similar in that regard, but our reliance on E_NOTICE to (ahem) enforce isset() checks is why, I
> >>> assume, we’re introducing the ?? operator to begin with.
> >>>
> >>> I just think it’d be nice to have a small variation on this feature that was
> >>> less concerned with a (defined|null) semantic and more in keeping with PHP’s overall handling of
> >>> implicit type conversion in expressions.
> >>>
> >>> Does that make more sense?
> >>>
> >>>> On Nov 22, 2015, at 12:41 PM, Andrea Faulds <ajf@ajf.me
> >>>> <mailto:ajf@ajf.me>> wrote:
> >>>>
> >>>> Hi,
> >>>>
> >>>> shadda wrote:
> >>>>> I had a question-suggestion based around the cool new operator we’re
> >>>>> getting in PHP7, the ?? operator, which as I understand it is the functional equivalent to perl’s
> >>>>> // operator; in that they both test for whether or not a variable is defined, rather than it’s
> >>>>> truthiness.
> >>>>
> >>>> This is not strictly correct. Though something of a misnomer, the 'null
> >>>> coalesce operator' checks if a variable doesn't exist, but also that it is not null, i.e.
> >>>> it functions like isset().
> >>>>
> >>>> Thanks.
> >>>>
> >>>> --
> >>>> Andrea Faulds
> >>>> http://ajf.me/
> >>>> <http://ajf.me/>
> >>>>
> >>>> --
> >>>> PHP Internals - PHP Runtime Development Mailing List
> >>>> To unsubscribe, visit:
> >>>> http://www.php.net/unsub.php
> >>>> <http://www.php.net/unsub.php>
> >>>>
> >>>
> >>
> >>
> >> --
> >> PHP Internals - PHP Runtime Development Mailing List
> >> To unsubscribe, visit: http://www.php.net/unsub.php
> >> <http://www.php.net/unsub.php>
> >>
> >
> > Ignoring E_NOTICE is generally considered bad practice by many, and
> > turning them off just to use ternaries instead of null coalesce (a
> > feature we already have) doesn't seem like a benefit.
> >
> > The ?? is used the same as "a = b || c" in Ruby or JS. Doing that in
> > PHP wouldn't work well as || works a bit differently, so ?? is used
> > instead, just like C and Swift.
> >
> > Basically... it's all good. And even if you hate it you're months too
> > late to raise a concern. :)
>
>
> --
> PHP Internals - PHP Runtime Development Mailing List
> To unsubscribe, visit: http://www.php.net/unsub.php
> <http://www.php.net/unsub.php>
>
>