Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:117155 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 30595 invoked from network); 27 Feb 2022 15:46:08 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 27 Feb 2022 15:46:08 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 65E541804C3 for ; Sun, 27 Feb 2022 09:06:53 -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=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,HTML_MESSAGE, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE, SPF_PASS autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS15169 209.85.128.0/17 X-Spam-Virus: No X-Envelope-From: Received: from mail-pg1-f174.google.com (mail-pg1-f174.google.com [209.85.215.174]) (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 ; Sun, 27 Feb 2022 09:06:52 -0800 (PST) Received: by mail-pg1-f174.google.com with SMTP id o26so8795537pgb.8 for ; Sun, 27 Feb 2022 09:06:52 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=mPngkEASY9Hq2B/rX8LsO34AsSfYqknaFLsnTZf1lwA=; b=S47dsAnxJaUkfZmQHKRLOwpMG+FQ+EYbySN73iZ88V+kAVK9aAbe4Ha4MbzxhzC0RW LTT2H+U/AudVsKW/wMUdVvgNGf7U4VfDN9Nt5MkkdifSypAUeng3ctJXY1Xo/H29YURp RK0R5jpLYhj0Fy5k5kVybzw6sb6I9xye6jj2yNtEcxM+YMlm7Q6UKasUPj2NsVewAp8V aRd+BQycVXTYko7cPhz3KRRz3RqdxINNf3EZjRQjl0syjCsZ7bxS4JpqUGaLnOYGTbJj Uk6TybRkVgM9GkqaMUIgUP/TMLAvtrrVK8yuqe8gQQQisLC1D4vWbDJXokr+pTUgrfHQ G7/Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=mPngkEASY9Hq2B/rX8LsO34AsSfYqknaFLsnTZf1lwA=; b=n3Zl4189FERN9FuFMbBB85n/i7xBQrvZ0zow/cz61ChsX69F/eeR4C6k27sejDjlHA SYjCYVvO8lcnGYPE2PhLvMqVSa4xYQZB1DXd41qQQ2ad34HZeM5EAEQVJQAiXDx+sXUj 5DgO+eHYAwGBruBkZf+sm2URieF0UdKi7t8fVEilCuGgDBT48WOxau1dbihdw2XRR8Pe BSJdXuAz97axukHIcBxAOW2QOW90rz2IFXmiqtoMTzVns7auZwlWIYctCQbkBwtLU1XN wbpBYtCHovbrjV+klIVYzkjcjmJuiiqrvLdMMeV6rxrsRcuzHNzFST8BUW0pQ/gri5US HxPA== X-Gm-Message-State: AOAM532nfHKD5k2Of9ceYbc5Sq17Ni2rwHZDNnyN+9HhiqyJuE+PRoXJ 5zqqvkEI6YfVz2BN7BdAAQguRZq2JB6zEfd6XgE= X-Google-Smtp-Source: ABdhPJwmgN5rP/FwpVkug/7Agaf4opbvFAqOpEFtJr6gLE7xt+0SyDvRo39YOGvfyT533Q3HdCp0+SYrsRKJb/4M8ps= X-Received: by 2002:a63:4665:0:b0:374:7305:dab4 with SMTP id v37-20020a634665000000b003747305dab4mr14035512pgk.256.1645981611525; Sun, 27 Feb 2022 09:06:51 -0800 (PST) MIME-Version: 1.0 References: <621a56dd.1c69fb81.67b1.242aSMTPIN_ADDED_MISSING@mx.google.com> <2CEDDAE8-76CF-4380-9E5A-0841EDD0B0C4@dafert.at> In-Reply-To: <2CEDDAE8-76CF-4380-9E5A-0841EDD0B0C4@dafert.at> Date: Sun, 27 Feb 2022 18:06:40 +0100 Message-ID: To: Mel Dafert Cc: internals , Kamil Tekiela Content-Type: multipart/alternative; boundary="000000000000430a9f05d902f591" Subject: Re: [PHP-DEV] Re: Proposal for RFC to remove undefined array index warning when used ina ternary From: landers.robert@gmail.com (Robert Landers) --000000000000430a9f05d902f591 Content-Type: text/plain; charset="UTF-8" On Sun, Feb 27, 2022 at 10:59 AM Mel Dafert wrote: > On 27 February 2022 00:04:58 CET, Kamil Tekiela > wrote: > >I just wanted to add that the following > > > >$name = $_POST['name'] ?: 'Default Name'; > > > >with existence check would be > > > >$name = $_POST['name'] ?? null ?: 'Default Name'; > > > >You don't need empty(). > > > >I would be against changing the behaviour of elvis/ternary operator. > >However, I remember seeing past suggestions to implement another operator. > >One that would fill the gap between null-coalesce and elvis operators. If > I > >recall correctly, most of the time, these proposals end in consensus that > >such operator isn't really needed. See > >https://externals.io/message/89292#89292 and > >https://externals.io/message/101606#101610 > >Maybe, it would be worthwhile to refresh a discussion about adding such > >operator to the PHP language? > > I think that what would be needed here is another proposal from this > discussion, > something like a null-safe operator but for array access. > > From a different thread about this: > > On 15 February 2022 16:50:48 CET, Rowan Tommins > wrote: > >I seem to remember someone proposing an explicit operator for "this array > dimension might not exist", something like this: > > > >if ( $array[?'key'] === true ) > > > >Which would translate to: > > > >if ( (array_key_exists('key', $array) ? $array['key'] : null) === true ) > > I would propose to extend this still to mean: > `if((($array !== null && array_key_exists('key', $array)) ? $array['key'] > : null) === true)` > > This combines the above proposal with the behaviour of the nullsafe > operator > `?->`, allowing to signal that the array may be null or that the key may > not be set. > > This allows the requested functionality without warnings/notices: > > $name = $_POST[?'name'] ?? 'Default Name'; > > But also allows accessing arrays with multiple, possibly defined > dimensions: > > $val = $array[?'a'][?'b'][?'c'] ?? 'default'; > > Currently, the last example would need to be a rather unwieldy ternary: > > $val = (isset($array['a']) && isset($array['a']['b']) && > isset($array['a']['b']['c']) ? $array['a']['b']['c'] : 'default'; > I've been writing it like this: $val = (($array['a'] ?? [])['b'] ?? [])['c'] ?? 'default'; which is still unwieldy. I don't actually know if it is required to use [] vs. null, but it makes sense to use an empty array to me. > > The fact that the new syntax would also allow $array to be null may be > undesirable in some cases, but I don't know if we can define the semantics > in a better way. > (Also, in some cases this may actually be useful, eg. when mixing ?-> and > the new > [?...] syntax.) > I am not sure if the exact syntax/semantics proposed here are the best > solution, but I would argue that the fact that this seems to be a > recurring topic > calls for a solution in that general direction. > > I've been playing with a couple of syntaxes all day, but I think $array[?$key] is growing on me. It isn't ambiguous or unwieldy. I did discover an interesting case though: ['inner key' => $value] = $array[?'outer key']; Right now ['key' => $value] = null; results in $value === null with no warning. I'd personally prefer for it to be a warning, but might be out of scope for this change, but if it were, maybe we'd want to use this syntax if we did want null. [?'inner key' => $value] = $array[?'outer key']; I don't think list() operations will be a part of this RFC, FWIW. > Regards, > Mel > --000000000000430a9f05d902f591--