Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:117452 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 46981 invoked from network); 30 Mar 2022 11:06:13 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 30 Mar 2022 11:06:13 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 203CC1804FF for ; Wed, 30 Mar 2022 05:34:42 -0700 (PDT) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_ENVFROM_END_DIGIT, FREEMAIL_FROM,HTML_MESSAGE,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE 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-ej1-f42.google.com (mail-ej1-f42.google.com [209.85.218.42]) (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 ; Wed, 30 Mar 2022 05:34:41 -0700 (PDT) Received: by mail-ej1-f42.google.com with SMTP id bg10so41215817ejb.4 for ; Wed, 30 Mar 2022 05:34:41 -0700 (PDT) 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=mZVWflKAHQoPFpNqe0OeLFvPAJdNWDNo2Cs5tfXwZMY=; b=I+sYLpwVtgRd7ruKifanX10VvbvgEKipU6rmNN6MFS0ITzKOb74OuY7XsxGstg4d9C VwgL3sDjoHMUcKg43WP6KEsUXGWDsWlyIDIOLuhuGpDgr8eJKEriHOx1J3LzKUXoFMr4 lob8KSBDsHYBu7mc02XR/6Yo/9zYtBIOc9ro2KqxCDBIQiCJJkpVP2kUk33OkdZuAYaK vx4PMZf06CBIZF0VfirYPF6Lzk9f2+EFP6PuO7YBRAOMSt4psc88b0torGg5dAsEnxlB dokOyR8CL+xn7VyxS+fpRZDxXid0C4SMeRk+Ryq9m8RCU/ddOR/XHEOOwfyaAw3CyJGt yS5A== 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=mZVWflKAHQoPFpNqe0OeLFvPAJdNWDNo2Cs5tfXwZMY=; b=pnFFEd5QoUvL6OV+a+Mli23AYhK+Aef7GyVyCfp5OdsT7m9sqi//x+VwXdwZU+5cBn FbZLF3G8gTU17xF9V9iLrf1Dsokvma17prfB0kMuabyqO7M5jJjSvD7xwW22UIGQkUMX LPLvQlH1inHDjRqWAKcpDEhaekJNE5Ym4B1QkpczfyOltIvuzLnIIjz42v6SmTbz4F80 RL2ir6hkbNpABTXzzNs5By435W4tX1397HjpHPAkGI7xW1J67x6hwyEdAp0eiRNfpMkY 6st/uhPP4c/Rsqirw8s+y5FdcFDqXhrXSfGvJjC1Sf3WCEYhQihTTaJ5ARg2v7mabe1X 74/Q== X-Gm-Message-State: AOAM531haze4A0Lu/bwBgEzMMkcpncFpBorRw2CJmKdeALkivX6WwDof hy4AAD5U6e46blV4qCwc5jQ04gq4q0qOoWwUVuY= X-Google-Smtp-Source: ABdhPJyAWDqbC44l9fnh/4L+bFdba84S6u1ZwrhRNMs44OyentvDl5sFrp04HMSFMfCtbG5iC4i6qsz3MJaLWN9bBNE= X-Received: by 2002:a17:907:7e97:b0:6db:c1ef:6a68 with SMTP id qb23-20020a1709077e9700b006dbc1ef6a68mr38746141ejc.475.1648643680317; Wed, 30 Mar 2022 05:34:40 -0700 (PDT) MIME-Version: 1.0 References: <9ecce8c9-c8bc-93e3-25f0-386c2c41ca1a@gmail.com> <92302377-6a49-d754-1210-b926c7381962@telia.com> In-Reply-To: <92302377-6a49-d754-1210-b926c7381962@telia.com> Date: Wed, 30 Mar 2022 14:34:04 +0200 Message-ID: To: =?UTF-8?Q?Bj=C3=B6rn_Larsson?= Cc: Rowan Tommins , PHP Internals Content-Type: multipart/alternative; boundary="000000000000ed37df05db6ec40b" Subject: Re: [PHP-DEV] Re: Undefined variables and the array append operator From: divinity76@gmail.com (Hans Henrik Bergan) --000000000000ed37df05db6ec40b Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable > RFC. There are other inconsistencies as well now, depending on how the undefined variable comes to exist absolutely, but shouldn't try to do too much in a single rfc, wouldn't want it to be rejected for the wrong reasons ^^ On Wed, 30 Mar 2022 at 12:17, Bj=C3=B6rn Larsson via internals < internals@lists.php.net> wrote: > Den 2022-03-29 kl. 21:44, skrev Rowan Tommins: > > Hi all, > > > > If $foo is not defined, statements such as $foo +=3D 1 and $foo .=3D 'b= lah' > > raise "undefined variable" Warnings in PHP 8, and will throw Errors in > > PHP 9. However, the very similar looking $foo[] =3D 1 succeeds silently= . > > > > This seems odd to me, as "append to string" and "append to array" seem > > like very similar operations, with most of the same use cases and > > possible bugs. > > > > > > From an *implementation* point of view, this is presumably because the= y > > are defined as different Op Codes - ASSIGN_OP for .=3D and ASSIGN_DIM f= or > > []=3D, I believe. But that doesn't explain *why* ASSIGN_DIM behaves thi= s > way. > > > > A *historical* explanation might relate to Perl's "autovivification" > > feature. However, PHP does *not* implement the same rules as Perl: for > > instance, Perl will create array dimensions just by reading them, which > > PHP does not; and Perl has a completely different type system, with > > cases like "a scalar becomes a reference to a hash unless already a > > reference to a list". Note also that I'm *not* talking about > > multi-dimensional arrays with missing keys here, only undefined local > > variables, as were the subject of the recent RFC. > > > > The *observable behaviour* for most operators in PHP is the same: > > > > 1) if the variable is undefined, consider the value to be null > > 2) coerce the value to the appropriate type; if the value is null, this > > gives the relevant "empty" value, such as '', 0, or [] > > 3) apply the operator > > > > There isn't anything particularly special with $foo[] =3D 'bar' in this > > case, except that it's the only operator that doesn't raise a Warning > > (and planned Error) at step 1. The same goes for all the other uses of > > the [] syntax I can think of, like $foo['key'] =3D 'bar', or $ref =3D& > $foo[]. > > > > > > For example, consider the following simple validation code > > [https://3v4l.org/pP5CU]: > > > > $requiredFields =3D ['name', 'age', 'hair_colour']; > > > > // Note: $errorString is not initialised > > foreach ( $requiredFields as $field ) { > > if ( ! isset($_POST[$field]) ) { > > $errorString .=3D "Missing required field '$field'. "; > > } > > } > > echo $errorString; > > > > This gives an "Undefined variable" Notice / Warning / Error, depending > > on the version. That's reasonable, as failing to initialise $errorStrin= g > > might cause problems if this code is integrated into a loop or larger > > function later. > > > > However, switch the code from building up a string to building up an > > array, and the result is identical, but the Notice / Warning / Error > > goes away [https://3v4l.org/ojZ1O]: > > > > // Note: $errorArray is not initialised > > foreach ( $requiredFields as $field ) { > > if ( ! isset($_POST[$field]) ) { > > $errorArray[] =3D "Missing required field '$field'. "; > > } > > } > > echo implode('', $errorArray); > > > > > > Can anyone give a compelling reason why we should keep this > > inconsistency, or should I raise an RFC to bring it in line with other > > undefined variable accesses? > > > > > I think it deserves an RFC. Then we also capture your excellent > explanation above! > > Regards //Bj=C3=B6rn L > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: https://www.php.net/unsub.php > > --000000000000ed37df05db6ec40b--