Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:117149 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 4938 invoked from network); 27 Feb 2022 07:52:12 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 27 Feb 2022 07:52:12 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 5CC66180507 for ; Sun, 27 Feb 2022 01:12: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=1.3 required=5.0 tests=BAYES_40,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,FROM_FMBLA_NEWDOM, 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-pj1-f45.google.com (mail-pj1-f45.google.com [209.85.216.45]) (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:12:52 -0800 (PST) Received: by mail-pj1-f45.google.com with SMTP id h17-20020a17090acf1100b001bc68ecce4aso12174675pju.4 for ; Sun, 27 Feb 2022 01:12: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=uF41L8sI1/zB/y+KYBw3SywFkOIE03SNPTPE8N7I7/o=; b=L7Q2ErSzIhut7i0ueIJ+gXTmwkvUyCejEkJ55WUm4TlEjs5X9J27lX7CmdVmZ8e74i H3HCQTsGmUlzPKtDaLX7iO1UKjZ+KZ2AHZusor5nlH6h5zhf23BMlRlcTSUpEQ4Ly1YG a+iAj3wLIkcfN44t1pWh8wZ1Z9SyHyoUb4XBrtsWFJES2iA+QC7TdK+hN0K7yKoHiVbx ohodA12d4TctLtttpb90tcDxa4MlKDgjW3FjlSXpH3pf/bjUi4IvnmftJJ4pXp5uKMdV GdkMnTZ5u9jQBBT2invYKNcjvowG0TR0KkkNC07atdeNXI8XzQpfDOMLY67jFtTVGCbr 5hQg== 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=uF41L8sI1/zB/y+KYBw3SywFkOIE03SNPTPE8N7I7/o=; b=hLTou21mnMBQOZNYp2of2oH4/4W+10/dNSPdcvmQt86DUZihBRcKsvP/IoBbGyvOMv rJaD7+bPzG0esLSMuJabpamVudt/OnvTOTAud0GssQqaInHG7wOSPxPko7DTyQEgJP3M Hdbvo1fYK3iMeoTUYfmkTn8VkZDMeXrEnRuEa/9/jAsKPJ3XcV+jzcqu8n1VsSdRLRbn w++ZBYiChRXrONPGh6eBOxsogMmGWe90WaLuEV5P7IOsjnXzMnAoIgDHZAhuKJuaSaAm kqj6HOw+ioNEDxxOzVUVt0ywSmGfFvq61gTSzXFoVoxgS7b5OAhGozNZq+AwW1y7p0TL xlWA== X-Gm-Message-State: AOAM5337veUtw13qfcJfvS3kw8fTXhiARNyTZC1LX+MuaDsiq+qUy1Nm h1Ktd5dSUkTpnTI2/g8opRkrlOYtP1Lii5/FkwiX452NtdPXLn6L X-Google-Smtp-Source: ABdhPJw2hd093wDC5e+31E5wItyRDw8Myqj/RiZlRQOWEXu4tPuESs6FzUU4pYBQJ9LjUu+5YygUn40x85xEL18Usi8= X-Received: by 2002:a17:90a:df16:b0:1bc:46ce:636a with SMTP id gp22-20020a17090adf1600b001bc46ce636amr11308889pjb.155.1645953170981; Sun, 27 Feb 2022 01:12:50 -0800 (PST) MIME-Version: 1.0 References: <621a56dd.1c69fb81.67b1.242aSMTPIN_ADDED_MISSING@mx.google.com> <621ab36a.1c69fb81.b1a5.462dSMTPIN_ADDED_MISSING@mx.google.com> In-Reply-To: <621ab36a.1c69fb81.b1a5.462dSMTPIN_ADDED_MISSING@mx.google.com> Date: Sun, 27 Feb 2022 10:12:39 +0100 Message-ID: To: Mark Randall Cc: internals Content-Type: multipart/alternative; boundary="00000000000012c79205d8fc56ba" Subject: Re: [PHP-DEV] Re: Proposal for RFC to remove undefined array indexwarning when used ina ternary From: landers.robert@gmail.com (Robert Landers) --00000000000012c79205d8fc56ba Content-Type: text/plain; charset="UTF-8" On Sun, Feb 27, 2022 at 12:10 AM Mark Randall wrote: > On 26/02/2022 22:34, Robert Landers wrote: > > This is not semantically the same though. A $_POST of a form, for > example, > > will usually contain an empty value if the form input was empty, but > > missing if the form control wasn't in the form (maybe the form control is > > injected via Javascript, or conditionally output by the script). > > > PHP is a general purpose language, the behaviour you're asking for is > specific to one use case and may be of detriment to others, without > offering significant benefit in exchange. > I suggest you write yourself a function that performs the operations for > you. This may also be a good opportunity to move away from directly > accessing the superglobals in userland code. > > $form->get('foo', 'defaultgoeshere'); > I gave that as an example, not a literal line from real code, just to show how common the situation is. Handling data structures like YAML, JSON, or parsing virtually any file format will run into this issue where you want to set a default if the value is falsy or non-existent. I've written a lot of this parsing-type of code over the years and run into this case at least once each time. > PHP is a general purpose language, the behaviour you're asking for is > specific to one use case and may be of detriment to others, without > offering significant benefit in exchange. I'd also venture that this warning has caused more harm than good, in that writing "$var['something'] ?? null" is second nature when writing new code, even if there is practically no chance for a non-existent key. This requires reviewers and authors to go digging into the source of the array and trace it to its usage, just to figure out whether or not a key can be non-existent under certain conditions (or discover a warning in production). In my experience, arrays are usually derived from user/machine input, which is impossible to predict the exact shape (outdated clients, malicious users, etc.) and the warning becomes noise with enough traffic. Perhaps a non-existent key should result in null by default (without a warning, but maybe a notice) unless you want a different value (using "??")? > > Performance concerns would be so so small they would be practically > undetectable in the context of a real request. > Also, this: > > $name = empty($_POST['name'] ?? 'Default Name') ?: 'Default Name'; > > Should be: > > $name = ($_POST['name'] ?? null) ?: 'Default Name') > > -- > Mark Randall > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: https://www.php.net/unsub.php > > --00000000000012c79205d8fc56ba--