Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:117038 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 45789 invoked from network); 15 Feb 2022 11:36:39 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 15 Feb 2022 11:36:39 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 2080918053C for ; Tue, 15 Feb 2022 04:54:22 -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.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,NICE_REPLY_A, RCVD_IN_DNSWL_LOW,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS8560 212.227.0.0/16 X-Spam-Virus: No X-Envelope-From: Received: from mout.gmx.net (mout.gmx.net [212.227.17.22]) (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 04:54:18 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gmx.net; s=badeba3b8450; t=1644929657; bh=cX2z2hj2zJxdJAnCMWI8pVsxG6GaDKO39linWq4m98s=; h=X-UI-Sender-Class:Date:Subject:To:References:From:In-Reply-To; b=O3E3uXps7yjx04yt1sS4Pj3Lf0B9xo/kO0j2rSn4dHG4LC94ZTfDRJnur3/3Jzliq ruSPj1lM/lc2/XIpVS0VoQ5BHpiQQCv4YBief78QzsMiD8PwuviA95mS25H/Jjyyaf VNysEeZPboU7sfARFNE17zLIV+t/EB1586bii5cI= X-UI-Sender-Class: 01bb95c1-4bf8-414a-932a-4f6e2808ef9c Received: from [192.168.178.120] ([24.134.51.41]) by mail.gmx.net (mrgmx104 [212.227.17.168]) with ESMTPSA (Nemesis) id 1McYCb-1nrejd3oOD-00cuha for ; Tue, 15 Feb 2022 13:54:16 +0100 Message-ID: <67207814-3052-b116-e856-33e8440049d3@gmx.net> Date: Tue, 15 Feb 2022 13:54:16 +0100 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.5.0 Content-Language: en-US To: internals@lists.php.net References: In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: quoted-printable X-Provags-ID: V03:K1:DzXPxXQMHyrcS9wH7JgHOiVhmKrm2guOod9cWbcSQl4bwoHFT68 BoI5RfGOGkESP7Dr0qEW0j6yHRreqCWTHN9tomlxxI4oLKKRlao48zH8vMHSBGWCgw+iRVd 2eJwlJCg6g8HB5Fc7gPtcs1l71yTa6xaWE2tMV0N5w8ZhBXCQVvEiTY2UH9zd8hcqcZvzIW 6WN7MyCtNWL0fOTMrkT0g== X-UI-Out-Filterresults: notjunk:1;V03:K0:LFYmbBAN/kE=:kFWZQPoYb6OzHtQm9Q/t7i LIltoJ/Tt1n3TdOzPBvKHi950diVMst7HfqzwwtOfB09gso8LqSf40ODF/BRb+j0ax6Ej989m y8ddgW0MKBG8Nq9xmwctOpLz9hI9QYq3HcelpBMDYVu40+F7TYQL6RxulY87P8wAyJp4EsEQ9 PUdBMuMwt7TT2/jYOfJGMZz2+yJRHlnje3BTz8pRMAOmiiz0MSGv5cYwIhBCOc36PGb7NVcKW cBF5BfQ906n1dnFIC8H7zyWCGTNX146zekVbf4MK4gNUUdHeojNA2F4Oxw4eDCFSa+USisp0J Zz3QCunBQZD2p8Jb3sDut7pwh3nabADzSLQWm7DinExokbjGDE+OXuWA1TtYaF32ZMPdgA7so DIErnk6nPWuUxPAitaf0zu+M2is0/ouqMRtdZyoIhuzbLLsN6OfUdJMsO4hY/6vI4628P+ypE 8i6jrtDqO50dbMag+FonjpIhjQkZrPjbly/bW/qRyTEc1GSvCmaaBvgbX7UV9lzkaPHeYY4eI Lfj/+eM7UFlYWMw1HmvI8qJ6CrvitihbtPxad7XmW/wzjlOcNmIjSp2Mh+dTN4zWd9/M9s+Xp XYTJpyssTxC+DIY944o1xPWRX4Vuhqs83vvP31/f/EM/Q6B8TGvSf8gregOV8spk4k7w6pyVG HczlN5m/LFrRSV2/Prw/i0zEF/mTurukB8rWnS2cfUWqHdOL94OCQcNfdz/pOxi20xNNHE/zv baX25+HPWPpbwnPLO31HMFw3RYceFwmYwSh1uHqQTjSZ2dzEwcfA7y9fN0ETTmi1PJLyubtyW FZuI6qvw/PDZXyypHxam09ehMOCVPMi+vR3AaGEIA3PisByYu4PXU8MYi/B/5+OlQw3wUPVTe dX7dGBGX6wAYB7jbGS6U18ziMkUPIzf51P8zmLcQ440QzTTm4NAVjax/KMYbSbASa7jOvWeTG PW7Sbh8Y/6DvjMf3gJslCu6caKPNF9vcC/SNZt8SrUa72rfImZ5Wb79fV5NgQg/GTxec9QiQw hsmmw/L9ZCVrfnMPyYsOz/e+cS7Q3D/soxC711TneJUNytZ0kqJirrlWfqOe5jgM83FDagJc1 BY/wUi/96I4M8M= Subject: Re: [PHP-DEV] Setting to disable the "Undefined array index" warning From: a.leathley@gmx.net (Andreas Leathley) On 15.02.22 13:31, Nicolas BADIA wrote: > As it is explained in this ticket https://bugs.php.net/bug.php?id=3D8141= 7 we use to check if a property exists by accessing it directly like we do= in JavaScript. > > Personally, I subscribe to this coding style and we use it all over our = codebase (more than 130 000 lines of PHP code). When it became a notice, w= e disabled them, but now that it is a warning, it is a lot more problemati= c for us=E2=80=A6 We can=E2=80=99t even use the last version of PHP Debug = for VSCode. The problem with your way of writing code is that it is ambiguous in meaning, which is why this is a warning. You are not checking if a key in an array exists, you are checking if an array value is "false-y". If the value is an empty string, it is also interpreted as false, if it is null, it is also interpreted as false, if it is an integer of value zero it is also interpreted as false, if it is the string "0" it is also interpreted as false. When you write code as you do, it is easy to introduce subtle errors because of these implicit casts. If you want to check if an array key is defined, you should do it explicitly - this is not about a "coding style", this is about writing the code in a way that actually does what you intend it to do without it meaning multiple things of which some are probably not expected. If you just want a quick fix without solving the underlying problem, you can just add "?? null" to each array key access. If you want to upgrade/improve your code base, which is a worthwhile task, I would suggest the use of a static analyzer (like Psalm or PHPStan) to find out where your code is ambigious. I have found many undetected errors that way, where implicit casts have lead to unexpected outcomes, which is why I am very much in favor of these warnings for undefined array keys.