Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:117150 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 7925 invoked from network); 27 Feb 2022 08:39:01 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 27 Feb 2022 08:39:01 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 6204F1804C3 for ; Sun, 27 Feb 2022 01:59:42 -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.6 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_LOW, SPF_HELO_NONE,SPF_NONE autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS8412 83.65.0.0/16 X-Spam-Virus: No X-Envelope-From: Received: from mail02.x-net.at (mail02.x-net.at [83.65.141.138]) (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 01:59:41 -0800 (PST) Received: from localhost (localhost [127.0.0.1]) by mail02.x-net.at (Postfix) with ESMTP id A60FD38009F; Sun, 27 Feb 2022 10:59:39 +0100 (CET) Received: from mail02.x-net.at ([127.0.0.1]) by localhost (mail02.x-net.at [127.0.0.1]) (amavisd-new, port 10032) with ESMTP id 7shssbczma5Z; Sun, 27 Feb 2022 10:59:37 +0100 (CET) Received: from localhost (localhost [127.0.0.1]) by mail02.x-net.at (Postfix) with ESMTP id 091613800C7; Sun, 27 Feb 2022 10:59:37 +0100 (CET) X-Virus-Scanned: amavisd-new at x-t.at Received: from mail02.x-net.at ([127.0.0.1]) by localhost (mail02.x-net.at [127.0.0.1]) (amavisd-new, port 10026) with ESMTP id 0I5hgiGSb0Lk; Sun, 27 Feb 2022 10:59:36 +0100 (CET) Received: from [127.0.0.1] (178.165.173.180.wireless.dyn.drei.com [178.165.173.180]) by mail02.x-net.at (Postfix) with ESMTPSA id 9F35638009F; Sun, 27 Feb 2022 10:59:36 +0100 (CET) Date: Sun, 27 Feb 2022 10:59:35 +0100 To: internals@lists.php.net, Kamil Tekiela User-Agent: K-9 Mail for Android In-Reply-To: References: <621a56dd.1c69fb81.67b1.242aSMTPIN_ADDED_MISSING@mx.google.com> Message-ID: <2CEDDAE8-76CF-4380-9E5A-0841EDD0B0C4@dafert.at> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Subject: =?US-ASCII?Q?Re=3A_=5BPHP-DEV=5D_Re=3A_Proposal_for_RFC_to_remove_und?= =?US-ASCII?Q?efined_array_index_warning_when_used_ina_ternary?= From: mel@dafert.at (Mel Dafert) On 27 February 2022 00:04:58 CET, Kamil Tekiela wr= ote: >I just wanted to add that the following > >$name =3D $_POST['name'] ?: 'Default Name'; > >with existence check would be > >$name =3D $_POST['name'] ?? null ?: 'Default Name'; > >You don't need empty()=2E > >I would be against changing the behaviour of elvis/ternary operator=2E >However, I remember seeing past suggestions to implement another operator= =2E >One that would fill the gap between null-coalesce and elvis operators=2E = If I >recall correctly, most of the time, these proposals end in consensus that >such operator isn't really needed=2E See >https://externals=2Eio/message/89292#89292 and >https://externals=2Eio/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 discu= ssion, something like a null-safe operator but for array access=2E From=20a 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'] =3D=3D=3D true ) > >Which would translate to: > >if ( (array_key_exists('key', $array) ? $array['key'] : null) =3D=3D=3D t= rue ) I would propose to extend this still to mean: `if((($array !=3D=3D null && array_key_exists('key', $array)) ? $array['ke= y'] : null) =3D=3D=3D true)` This combines the above proposal with the behaviour of the nullsafe operat= or `?->`, allowing to signal that the array may be null or that the key may n= ot be set=2E This allows the requested functionality without warnings/notices: $name =3D $_POST[?'name'] ?? 'Default Name'; But also allows accessing arrays with multiple, possibly defined dimension= s: $val =3D $array[?'a'][?'b'][?'c'] ?? 'default'; Currently, the last example would need to be a rather unwieldy ternary: $val =3D (isset($array['a']) && isset($array['a']['b']) && isset($array['a= ']['b']['c']) ? $array['a']['b']['c'] : 'default'; The fact that the new syntax would also allow $array to be null may be und= esirable in some cases, but I don't know if we can define the semantics in = a better way=2E (Also, in some cases this may actually be useful, eg=2E when mixing ?-> an= d the new [?=2E=2E=2E] syntax=2E) 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 recurrin= g topic calls for a solution in that general direction=2E Regards, Mel