> On Feb 23, 2021, at 2:05 PM, Rowan Tommins <rowan.coll...@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.

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

Reply via email to