Hi, On Fri, Feb 12, 2021 at 8:45 PM Ben Ramsey <b...@benramsey.com> wrote:
> > I think it might be a good idea to check other languages to see if they > support something like this. We could use their examples as points of > reference for discussing whether to include this functionality in PHP. > On Sat, Feb 13, 2021 at 5:22 PM David Rodrigues <david.pro...@gmail.com> wrote: > > One of the examples I have is when I need to include a class in an HTML > element conditionally. There are probably better ways, but this is a quick > one for simple situations: > > <div class="user {{ $user => "online" }}">...</div> vs. > <div class="user {{ $user ? "online" : null }}">...</div> > On Tue, Feb 23, 2021 at 3:31 PM Nikita Popov <nikita....@gmail.com> wrote: > > There's a limited budget for this kind of syntax sugar, and we already have > ?:, ??, ??=, ?->. I don't think there's space for yet another shorthand > conditional syntax. > > Note that => cannot be used for this purpose, as it is already used for > array literals. > In Twig <https://en.wikipedia.org/wiki/Twig_(template_engine)>, the "else" part of a ternary expression is optional (colon included) and defaults to an empty string. So, the following two lines are equivalent: <div class="user {{ user ? 'online' }}">...</div> <div class="user {{ user ? 'online' : '' }}">...</div> Maybe PHP could do the same (with `null` rather than an empty string for the default), i.e.: var_dump(true ? 'foo'); // string(3) "foo" var_dump(false ? 'foo'); // NULL but I'm not sure if the added complexity (in parser implementation and in the language) would be worth it, to avoid typing " : null", nor if everyone would actually find the shorthand more readable? -- Guilliam Xavier