Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:117041 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 54750 invoked from network); 15 Feb 2022 12:49:56 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 15 Feb 2022 12:49:56 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 8E16018054D for ; Tue, 15 Feb 2022 06:07:39 -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.5 required=5.0 tests=BAYES_50,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-wr1-f53.google.com (mail-wr1-f53.google.com [209.85.221.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 06:07:39 -0800 (PST) Received: by mail-wr1-f53.google.com with SMTP id s10so18796639wrb.1 for ; Tue, 15 Feb 2022 06:07:39 -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=rtvV5dw7eLlS3Y5nKntugl4ErMiX28ZqqLP7IhaRS2k=; b=hA0yawLR9gli99Hdf4/moXndgga3CwYLec+T8BlGD8sWcBx88Fbx/NLPmKkrR5Vr2n wl3dFhH6AXha4nQ3ki8Tv4pKd353UuHRVbgvSsuygJz138D2TonGZO81s0Nv3MR7XYNu kHb+YEN8zqI7mZIrZ2awBBU7SPHJriwlaRpAunqJeruBKiKEgZmH1XfHygwC9ZRymh2H beHExyBq7XPgKKw7lSczDaw4GMnzcxquF6LK1BPoFiOlMjoe2c1Com8RSVfu+jxdZ9P4 xrYT8ddH+Vmd6v/20M3rmAcTPHrSC6c4cA5R9hTD6wUNmvPhYLmzWsLAFCjb8zqEV/Vj heNA== 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=rtvV5dw7eLlS3Y5nKntugl4ErMiX28ZqqLP7IhaRS2k=; b=tIJBEjLuI8fJQs1+2gNPMUARKTdbxf29oj8lzeyuuUIGjR0RaKqKnvx2K6bb2i4XF4 rci+7qG/B6MmjVjap8jwwyzgPksUbwOPVnOSvNW1cbMr9ZvwJOmHRcjBE7KKtVYwCBQp yYbw0KLZeL3wATOQxXH1Ke4E0sxKU4EOgPUPVEXbbJfKB/TM/yyN2EhwIUHLDlSN8znV ChrZksljAJ1laPdHAeizfc57noLknm6iyaDLtj/MuG5b+1ZAYYamxOUprhFIQbDJ50CD hMfyJEwE5frMUye9QxirjjSbtIbLkeu54cpduY4FJk+wLIK76xrfEWNy0O0mDfZWhZbf 3sFA== X-Gm-Message-State: AOAM531LMFTDrmP61ei61dLJS03OGJpQEQAZZTmr83t45gAQ8fAG8K0d Bx1RFpRjLKcUM/NIz1H7SdtwIJVr6GSMbg== X-Google-Smtp-Source: ABdhPJwTLPyt0gbRGGCgl6UAAoL60lfAETpQ1+k5fOcp9WtXnA96NY6DBMOcR1oVYKaaP+GJ6UkbEA== X-Received: by 2002:a5d:5850:: with SMTP id i16mr3401738wrf.64.1644934057975; Tue, 15 Feb 2022 06:07:37 -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 g5sm15135136wmk.38.2022.02.15.06.07.36 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 15 Feb 2022 06:07:37 -0800 (PST) Message-ID: <51cf7bdf-450d-657d-cb1e-d52c3c4563f9@gmail.com> Date: Tue, 15 Feb 2022 14:07:36 +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> In-Reply-To: <67207814-3052-b116-e856-33e8440049d3@gmx.net> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Subject: Re: [PHP-DEV] Setting to disable the "Undefined array index" warning From: rowan.collins@gmail.com (Rowan Tommins) 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'] !== [] ) 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 ) Regards, -- Rowan Tommins [IMSoP]