Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:117042 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 60173 invoked from network); 15 Feb 2022 13:55:03 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 15 Feb 2022 13:55:03 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 6E15C18053E for ; Tue, 15 Feb 2022 07:12:47 -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=-0.2 required=5.0 tests=BAYES_20,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,HTML_MESSAGE, 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-lf1-f54.google.com (mail-lf1-f54.google.com [209.85.167.54]) (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:12:44 -0800 (PST) Received: by mail-lf1-f54.google.com with SMTP id u6so37479369lfc.3 for ; Tue, 15 Feb 2022 07:12: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; bh=oTApTKwBg0b6a0DktRBzfY7711SMsoicah9izbo6MB4=; b=PBotCjwC7q05mgWQAozHcpxxRAAwdQkTCtvvY7YIMmcbtUsmo6RlUUfqqQ4bS5fsx9 W+QqQe7MaGDHs8lxr04b20J1kP0kfoV+evq8jSYXVSUFe/Qw9EZiwd9IGAxgD2AqYsHE 7tgJEfnkpPj+5UVueQeBsAPTjhVKe4rn2vaMY0XE0T2lUQX2Oy/mh0/4g4BRRnmgX0WK n1BQ2P7dWFuPlgex34ssL0SGi0MX5rgZwPXXNXGJjxi8VFvve6Q3WbWRyTJibISWXsKf v/4c0rxVfD6KQ34Hq/vIjZ3aIknTrmrBLjnDO5FBZChkFClxWogOaG+tnUxlGFymi4Qj uKFA== 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; bh=oTApTKwBg0b6a0DktRBzfY7711SMsoicah9izbo6MB4=; b=3Y5eGRyRyRKCxNtf63uAK558HsnKkqQgYNQOdnWtiNrjfO83ilC79/b96jT7Y/Vl3D 1igxLSG93QCZ1Sc2/FuROq1GY3aqmnjdtPUSF/3caIfAqZXRVQHTYxaKcLxOxRbVut+h 6nZ/E4ammdjjRN6Nj3cRyjSzECNkjvJcTKGWLzV91sUrdt3kez6R2GK4b/y77YUCjacr RdWcw3NkMXb5fwf64533DhTnLlN2dDQ1v2jG9vzBwJ2C+YW8AaeVzvuS5hou2az6vGvD f91oRs7MYiu6o874NFp9c3V4AtMcfdzUc5kshftMI3/yufN82fScvY7UO97680WK+bWP bsbA== X-Gm-Message-State: AOAM53184Oga6scdgW7avrvtOUwutxLyIHnGDvcVu1yzAy5txpm5xBW6 ciYOW77tCgLgLb2j3akBejjO3XMiCzVqRZhdhl0+Ycn3Ww== X-Google-Smtp-Source: ABdhPJz06VQfF3woLYjV/IItXBpMaMkq67gtPgf7IQOsLo5GR1zyzcVp643U7cfRhtRyHEUuBYgY/wlV+vGTDtMxkNk= X-Received: by 2002:a05:6512:2342:: with SMTP id p2mr3517726lfu.348.1644937962358; Tue, 15 Feb 2022 07:12:42 -0800 (PST) MIME-Version: 1.0 References: <67207814-3052-b116-e856-33e8440049d3@gmx.net> <51cf7bdf-450d-657d-cb1e-d52c3c4563f9@gmail.com> In-Reply-To: <51cf7bdf-450d-657d-cb1e-d52c3c4563f9@gmail.com> Date: Tue, 15 Feb 2022 16:12:31 +0100 Message-ID: To: internals@lists.php.net Content-Type: multipart/alternative; boundary="000000000000ec919b05d80ff6fe" Subject: Re: [PHP-DEV] Setting to disable the "Undefined array index" warning From: guilliam.xavier@gmail.com (Guilliam Xavier) --000000000000ec919b05d80ff6fe Content-Type: text/plain; charset="UTF-8" On Tue, Feb 15, 2022 at 3:07 PM Rowan Tommins wrote: > On 15/02/2022 12:54, Andreas Leathley wrote: > > The problem with your way of writing code is that it is ambiguous in > > meaning, which is why this is a warning. > > > I think that's a good way of looking at it. There's actually quite a lot > of code hiding a check like "if ($array['key'])"; roughly speaking, it's > short-hand for: > > 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' > Now, if that's what you intended, there's a syntax that's slightly more > explicit while still being reasonably short: the empty() pseudo-function: > > if ( ! empty($array['key']) ) > > On the other hand, if what you actually meant was this: > > if ( array_key_exists('key', $array) && $array['key'] !== null ) > > Then you might have some bugs lurking, and actually want the isset() > pseudo-function instead: > > if ( isset($array['key']) ) > > For other cases, you do have to spend a few more characters to be > explicit, often using the "??" operator; for instance, if you expect > $array['key'] to be either unset or a boolean, and want this: > > if ( array_key_exists('key', $array) && $array['key'] === true ) > > Then the shortest is probably something like this: > > if ( $array['key'] ?? false === true ) > 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 ) I will also note that the "shorthands" (!empty(), isset(), ?? null) will also "hide" another warning when the $array variable itself is undefined --000000000000ec919b05d80ff6fe--