Re: Inline conditional that returns null if falsy

From: Date: Thu, 25 Feb 2021 08:55:07 +0000
Subject: Re: Inline conditional that returns null if falsy
References: 1 2 3 4 5 6 7 8  Groups: php.internals 
Request: Send a blank email to internals+get-113256@lists.php.net to get a copy of this message
On 24/02/2021 04:26, Mike Schinkel wrote:
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>
That's an interesting use case. Of course, the logical default there is actually '' (empty string) rather than null, since "echo null;" doesn't actually make much sense, but null works under current type juggling rules. Then again, perhaps this is a good example of why PHP *isn't* the best choice any more if you want a templating language. Twig, for instance, has exactly this facility [https://twig.symfony.com/doc/3.x/templates.html#other-operators]: {{ foo ? 'yes' : 'no' }} {{ foo ?: 'no' }}is the same as {{ foo ? foo : 'no' }} {{ foo ? 'yes' }}is the same as {{ foo ? 'yes' : '' }} If we wanted to close the gap between Twig and plain PHP for templating, this might be somewhere on the list, but after more significant features like automatic escaping, filter / pipe syntax, etc. Regards, -- Rowan Tommins [IMSoP]

Thread (23 messages)

« previous php.internals (#113256) next »