Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:113225 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 82019 invoked from network); 23 Feb 2021 15:37:54 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 23 Feb 2021 15:37:54 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 26B661804B3 for ; Tue, 23 Feb 2021 07:26:25 -0800 (PST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-0.2 required=5.0 tests=BAYES_40,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,HTML_MESSAGE, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.2 X-Spam-Virus: No X-Envelope-From: Received: from mail-lj1-f182.google.com (mail-lj1-f182.google.com [209.85.208.182]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Tue, 23 Feb 2021 07:26:24 -0800 (PST) Received: by mail-lj1-f182.google.com with SMTP id c17so63580632ljn.0 for ; Tue, 23 Feb 2021 07:26:24 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to; bh=JT2hcfYcyK5fOU2rzD79uVTOGp4OqkgvUM6hNEdKs8k=; b=XyJiltpCPqj29dcsdSg0qo54wLmILyh/2kJf+/8Fm0YcvEUwCkeGl6J2kh4YUM7SzR tgSfRveUNsAJghZ1InxImhdlRu53hiCRJSVsMcpcZVpztQgVbHP8WTlVgrFUqQfRHMDa /achDDV5Q8vtcQTJ3rQ+Gukl4/DfmpEgik+/1vJCDLHjhVEj4h8xQWrRNX+Sv1YERrFj hmQN+b9v0808YV1vizkmv8m5xlz1/EWfge5uEk2ySMNuqbzwCsZbTe6wqxyvXGvoTlI0 OrlSN7dyHyO+SpDfmICoVvALo3CPXxS8cjpwnDSxdUpZ0y19TKc6MINFDdCQds1HnDaz WsQA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to; bh=JT2hcfYcyK5fOU2rzD79uVTOGp4OqkgvUM6hNEdKs8k=; b=kXGt8SJsuucuzUYFe1HrulD44hCIM06J4VUM+eruvoP/tZy970qBCzqF5g6iSQTIYp kcq6nXfrF/WSIlC3evmNIkGjEK8bnY5NB8ew6eWFQ9FW0uAebWomF0tDq+5Ka50qwh9V uOzSaqo6NDpUfj3Q+UXpABnB5GRgpQhmZNQz8IrIG52+QWyDQZk/SC2qiGGTLbtXAc3I 6apela+QHzDl3Zw3yT8vg+kwZNg1GFUL7BXZoGXvCPgigjfOm+l4Xhfyr+TGp+qDVkW6 VgpZy3hR2oI5JJxfIGlTYvewMV7GEmsuCRkUzFjba5RYne+j1j7ZpZwFmpT7+O/XTG4p eRiA== X-Gm-Message-State: AOAM530/koG7HOjuhLJNgAETYEBcHi0PR3z9sPUSdeUhhOuG/Q5QzvgP NC3R/OcO8EfwTIv6k47lWVMJ7gWA2ILfUxED/UnVCJ5RiG4H X-Google-Smtp-Source: ABdhPJxAnx9xGlZS5ues2NV/2xl+6eOheRq6/BqMjvyHg2k5pS20rJY6wsJSVEvSdilyXoVZCwIZgGEFXBkOg/oJxS4= X-Received: by 2002:a2e:b17b:: with SMTP id a27mr5191941ljm.128.1614093981416; Tue, 23 Feb 2021 07:26:21 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: Date: Tue, 23 Feb 2021 16:26:11 +0100 Message-ID: To: PHP Internals Content-Type: multipart/alternative; boundary="0000000000006580da05bc028a4d" Subject: Re: [PHP-DEV] Inline conditional that returns null if falsy From: guilliam.xavier@gmail.com (Guilliam Xavier) --0000000000006580da05bc028a4d Content-Type: text/plain; charset="UTF-8" Hi, On Fri, Feb 12, 2021 at 8:45 PM Ben Ramsey 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 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: > >
...
vs. >
...
> On Tue, Feb 23, 2021 at 3:31 PM Nikita Popov 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 , the "else" part of a ternary expression is optional (colon included) and defaults to an empty string. So, the following two lines are equivalent:
...
...
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 --0000000000006580da05bc028a4d--