Re: Inline conditional that returns null if falsy
| From: | Mike Schinkel | Date: | Wed, 24 Feb 2021 16:35:37 +0000 |
| Subject: | Re: Inline conditional that returns null if falsy | ||
| References: | 1 2 3 4 5 6 7 8 9 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-113248@lists.php.net to get a copy of this message | ||
> On Feb 24, 2021, at 11:27 AM, Chase Peeler <chasepeeler@gmail.com> wrote:
>
> On Tue, Feb 23, 2021 at 11:27 PM Mike Schinkel <mike@newclarity.net
> <mailto:mike@newclarity.net>> wrote:
> > On Feb 23, 2021, at 2:05 PM, Rowan Tommins <rowan.collins@gmail.com
> > <mailto:rowan.collins@gmail.com>> wrote:
> >
> > On 23/02/2021 18:41, Albert Casademont wrote:
> >> Sure, it's not a big deal having to write the ": null" but it
> >> doesn't add
> >> any value
> >
> >
> > On the contrary, it adds an important piece of information: that the default value is
> > "null", rather than "false", or "0", or "new EmptyValue()".
> > For instance, it doesn't seem at all obvious to me that this code should produce a
> > null:
> >
> > $items = [];
> > $itemCount = $items ? count($items);
> >
> > I might be more convinced that "null" is the "natural" value if the
> > left-hand operand was only checked against null, not "falsiness": in that case, you can
> > read it as "if it's null, leave it alone". I'd still be inclined towards
> > "too specific to use up more syntax", though.
>
> If you look at it from a software engineering perspective — which is how I assume most on
> this thread have been looking at it — being more explicit in the code is probably a better
> practice than saving a few keystrokes.
>
> OTOH, if you view it from the perspective of a *templating* language — which is what PHP was
> initially created to be, for the web — then the reduction in visual noise from omitting ":
> null" would be a nice plus. And in the case of templating, the distinction between null vs.
> false vs. "" would be completely moot.
>
>
> But PHP isn't just a templating language anymore.
It is not? If it is not I think a lot of userland PHP developers never got that decree.
> If PHP currently had support for omitting the third argument on the ternary operator, then
> I'd be against changes that required it given the BC implications.
Can you elaborate on those?
> I think just dropping the need for a third argument on the existing ternary operator is a bad
> idea.
>
> I'm ambivalent when it comes to creating a new symbol to support this behavior (e.g. ?!).
> I don't really think it's necessary, but, I don't think it hurts anything either.
>
>
> Repeating code similar to that from the message sent by David Rodrigues you can see that
> eliminating the ": null" is less noisy:
>
> <div class="user <?php user ? 'online' : null
> ?>">...</div>
> <div class="user <?php user ? 'online' ?>">...</div>
>
> Although one ":null" eliminated is not that significant, PHP templates for
> generating HTML are notorious for having conditionals littered throughout, with tens if not hundreds
> of cases per file. So being able to drop the ":null" would be a really nice addition for
> templating.
>
> To emphasize this feature address templating use-cases one might argue that dropping the
> "or" case of the trinary might only be supported when between single-line
> "<?php" and "?>" delimiters.
>
> -Mike
>
> P.S. Of course making it work differently between single-line delimiters and elsewhere would
> create inconsistency in the language so I probably would not actually argue that, I was just trying
> to make a rhetorical point.
>
> --
> PHP Internals - PHP Runtime Development Mailing List
> To unsubscribe, visit: https://www.php.net/unsub.php
> <https://www.php.net/unsub.php>
>
>
>
> --
> Chase Peeler
> chasepeeler@gmail.com <mailto:chasepeeler@gmail.com>