Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:117145 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 80102 invoked from network); 26 Feb 2022 21:16:09 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 26 Feb 2022 21:16:09 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 7D6C51804C3 for ; Sat, 26 Feb 2022 14:36:43 -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-pf1-f170.google.com (mail-pf1-f170.google.com [209.85.210.170]) (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 ; Sat, 26 Feb 2022 14:36:43 -0800 (PST) Received: by mail-pf1-f170.google.com with SMTP id u16so7735655pfg.12 for ; Sat, 26 Feb 2022 14:36:43 -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=sMRewftbZKEZO31B840hraUxtieZhd3iBqQ5qgFwFtE=; b=Gybl45ssVSys72cdrgoOuubntSU7lkxZM3EH8oIbj0CjeE9R1uu8aMS3JFT81eyKh6 CgXy0y2Rlguz5P6Rx9HxGE2RS1H0GpgelxJJVLc3bnJiy7umjVtMBhxPBvZBvy7z+H35 UOI1VGUMJYVlFYaD4kzw2L6co8tSFu8kXRCoxqAyD4TW0sNJHWz42Y7xu9sGMq75SI/9 u9DGk7ndXGrBWwyY+Zj6Nnb8fo4m4Qecs9gVBjP2OV7y0HcdiZoMU23fGnjI216fn73x pgECBI4iyhl6I0Cgltu6qd/KY3GHx251VOkxc+Hb/NtRZb3rw0T9gjFh6ay8eYNOhMdo 1Xrg== 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=sMRewftbZKEZO31B840hraUxtieZhd3iBqQ5qgFwFtE=; b=5fBoVBM/1ZJW3D741hKQU+RUuwg4PGCMI9Ai/BZNPufLAToNCzF1cFB2NqeP5Mz8UO XntV7PxcmIeOpydmNOmnraKRPIMQP6nf8BnQF3Ctzk1D3lt1Dsc2vSwRu74pvo92Xrhz I3NQLUgDmpxF35dl9GRIH6gALGnPIY5RhZuuQEKb32WabnTHn/9w4SxDBvyBTmLdOD0h gtdA0knbfaTEXQEwUsol9h+NNclG0TBi3bMyE6BCHv729siezsEjiQgagWnY+IGGX7CC 1oEfGIBzAiLAIt8Nw1y/3rezOEpV5N+lxIRZLdY78GDGvbW7FvsPfkcLRu+fBWBFPnvg 7M5g== X-Gm-Message-State: AOAM530IWMQnvKS+eWLO24hd06WFlMtZwQRl8C8b+RCjt7y8InBZUjS4 N9/bSt6CoR+nQTL7/i6IfDSM5oBVyWqQAkB+d3A5XqIOCDr4Dg== X-Google-Smtp-Source: ABdhPJzw7aoH+uXvz7R4zPsPskg2lPOmAierRLWatOQpzHMEHh3Rq5NoE5bPcqYDzkEUT1hEi9wvYYKtLcqxaKgJj5Q= X-Received: by 2002:a05:6a00:1307:b0:4b0:b1c:6fd9 with SMTP id j7-20020a056a00130700b004b00b1c6fd9mr14438945pfu.27.1645915002192; Sat, 26 Feb 2022 14:36:42 -0800 (PST) MIME-Version: 1.0 References: <621a56dd.1c69fb81.67b1.242aSMTPIN_ADDED_MISSING@mx.google.com> In-Reply-To: Date: Sat, 26 Feb 2022 23:36:31 +0100 Message-ID: To: Mark Randall Cc: internals Content-Type: multipart/alternative; boundary="00000000000009462705d8f373d6" 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) --00000000000009462705d8f373d6 Content-Type: text/plain; charset="UTF-8" On Sat, Feb 26, 2022 at 11:34 PM Robert Landers wrote: > On Sat, Feb 26, 2022 at 5:35 PM Mark Randall wrote: > >> On 26/02/2022 09:09, Robert Landers wrote: >> > I'd like to propose and implement an RFC that would consider dropping >> the >> > warning if and only if array access is the only test in ternary and >> short >> > ternary (?:) operations but first I'd like to start a discussion to see >> if >> > it is worth pursuing. I'd like to target PHP 8.2 or 8.3. >> >> The warning comes from the array key being missing, not from being >> empty. There are many values which are empty which do not emit the >> warning. >> >> If you want to provide a default should the array key be missing you >> want null coalesce. >> >> $x = $array['foo'] ?? 'somethingelse'; >> >> > 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). > > Here is how the code might look currently to handle a default value for an > empty form post that may or may not be sent in the request (ignoring how > insecure this looks): > > $name = $_POST['name'] ?: 'Default Name'; > > Semantically the same to get rid of the warning: > > $name = empty($_POST['name']) ? 'Default Name' : $_POST['name']; > > I'm not sure your suggestion to set a default applies since we have to do > the empty check anyway, which swallows the warning? > > $name = empty($_POST['name'] ?? 'Default Name') ?: 'Default Name'; > The fact that I just wrote ^^ that bug only to realize as soon as I sent it should underscore how error-prone this migration could be. > > >> >> -- >> PHP Internals - PHP Runtime Development Mailing List >> To unsubscribe, visit: https://www.php.net/unsub.php >> >> > Robert Landers > Software Engineer > Utrecht NL > > > --00000000000009462705d8f373d6--