Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:117043 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 63650 invoked from network); 15 Feb 2022 14:33:07 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 15 Feb 2022 14:33:07 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 9B77B180544 for ; Tue, 15 Feb 2022 07:50:51 -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.2 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,NICE_REPLY_A, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,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-wm1-f53.google.com (mail-wm1-f53.google.com [209.85.128.53]) (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 ; Tue, 15 Feb 2022 07:50:51 -0800 (PST) Received: by mail-wm1-f53.google.com with SMTP id m126-20020a1ca384000000b0037bb8e379feso1763229wme.5 for ; Tue, 15 Feb 2022 07:50:51 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=message-id:date:mime-version:user-agent:subject:content-language:to :references:from:in-reply-to:content-transfer-encoding; bh=lTo5whnkANgwtLBgg5vhcX/qv/iKz//9M8uoeFlV/qo=; b=TUYlVIr+32CYc9c3Wn/glon48nIp56H0UqCKcYz92jgG+R5N1p7HIiwIkRsRkvumln /YcI3FUi28RbNMGh0eB3sCuRRM7bYF7rpvNWSmSgf47ElzrobJI/cADuYyThRt2DI+Iu SKrCwmkq2zwjAOeYbp4blLzvjeYrhfl3RbfBLrpC4FWZwCMvGJKcLbII7XCK8bgNY6aH g4qlAO0w8iX6gyAVVTbF0AWZ9g5vB3GutPPcskkSGlBusRG7KYBIkti81v8h0HPbQcXM d5pYdAOUmOgl+Ghh+DGyJa07BPBZ5X73RXjFNBpz6oaRaXhiHKJ1c89iT8DeaFdVUSbv Rk7g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:date:mime-version:user-agent:subject :content-language:to:references:from:in-reply-to :content-transfer-encoding; bh=lTo5whnkANgwtLBgg5vhcX/qv/iKz//9M8uoeFlV/qo=; b=R/YqNxxJ+8zWORkuoMbrzRtjiMctQrW3i7rGF1VWcPpSK/X0dhlLyQGt3J8LyJEDBY rYbcx4q5FZtqJNcLOEJgMoruZ3ZsNDwcX0WjWojICOkF8x1pn8bBzNc7PhrKYB/w1N5e 0DGOqLcypiEy3lpqVu3hL84FTEBkqEQA2CkU3ZrOkrNQ4AJLcTr2lyC6kWl8gcOW0UEf 26oiHBW3NSA5ISvudIzYtRsBHYsN3omWnXIPu53uehnWL+jH38a9U/usrPrJBr1gCbw6 QFXfhy2bYFdst0sGTFbAUTsk5jEOIXsY3wEhLZs7eZ+XZxkfSiXPVuDEANxnEEKA80Zq AXKw== X-Gm-Message-State: AOAM530NEJDSJSkYPg6pFQoGsuQhY3JdnD3QaQK8gMnIssVaQm8Yh+K+ B5j/aLTswjv9xaodVQ+JzU+NQ1SmUKYmAA== X-Google-Smtp-Source: ABdhPJzufR11XmOg9ObOpD27KTi1Uo+BDbXnGOmUl1gkiO2V/rmG0sDslmULd2xPdv+g1SgCn9zqZg== X-Received: by 2002:a7b:cc81:: with SMTP id p1mr3605288wma.159.1644940249859; Tue, 15 Feb 2022 07:50:49 -0800 (PST) Received: from [192.168.0.22] (cpc104104-brig22-2-0-cust548.3-3.cable.virginm.net. [82.10.58.37]) by smtp.googlemail.com with ESMTPSA id o14sm15353756wmr.3.2022.02.15.07.50.49 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 15 Feb 2022 07:50:49 -0800 (PST) Message-ID: <823b7b00-923e-ed68-038a-090a07d78c04@gmail.com> Date: Tue, 15 Feb 2022 15:50:48 +0000 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101 Thunderbird/91.6.0 Content-Language: en-GB To: internals@lists.php.net References: <67207814-3052-b116-e856-33e8440049d3@gmx.net> <51cf7bdf-450d-657d-cb1e-d52c3c4563f9@gmail.com> In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Subject: Re: [PHP-DEV] Setting to disable the "Undefined array index" warning From: rowan.collins@gmail.com (Rowan Tommins) On 15/02/2022 15:12, Guilliam Xavier wrote: > On Tue, Feb 15, 2022 at 3:07 PM Rowan Tommins > wrote: > >> if ( array_key_exists('key', $array) && $array['key'] !== null && >> $array['key'] !== false && $array['key'] !== 0 && $array['key'] !== 0.0 >> && $array['key'] !== '' && $array['key'] !== [] ) >> > Agreed, but you forgot the most "annoying": > > && $array['key'] !== '0' Ah yes, that one really demonstrates the dangers of type juggling in general. If it _wasn't_ considered empty, that would be inconsistent with how integer juggling works, but it definitely catches people out! > Mind operator precedence! This is interpreted as: > > if ( $array['key'] ?? (false === true) ) > > (obviously not intended), so you need explicit parentheses: > > if ( ($array['key'] ?? false) === true ) Oops, I admit I didn't test that. That does make the operator somewhat less attractive, and maybe it would be more straight-forward to just be explicit: if ( isset($array['key']) && $array['key'] === true ) > I will also note that the "shorthands" (!empty(), isset(), ?? null) will > also "hide" another warning when the $array variable itself is undefined Ah, yes, so they do; as does the ?? operator, for that matter. 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 ) Regards, -- Rowan Tommins [IMSoP]