Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:119141 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 27188 invoked from network); 13 Dec 2022 23:27:41 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 13 Dec 2022 23:27:41 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 1CE661804FF for ; Tue, 13 Dec 2022 15:27:41 -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_H2,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE 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-f49.google.com (mail-wm1-f49.google.com [209.85.128.49]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Tue, 13 Dec 2022 15:27:37 -0800 (PST) Received: by mail-wm1-f49.google.com with SMTP id bg10so9904245wmb.1 for ; Tue, 13 Dec 2022 15:27:37 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=to:subject:message-id:date:from:in-reply-to:references:mime-version :from:to:cc:subject:date:message-id:reply-to; bh=ASB97v3WCIvNTcIVuNopzS7yi91aNSltn3kSdXe7kxo=; b=jmxpQisNS2dAhta2fhhGD777dJ9Y1JCvRFTSMcGLYMRBbczz0e3BbrX9u9lSqCUTTg VEsnGg8qn0ZTcG89L3bX5dtGV1vj+GKU2F6gPFKJZo0Z513kx2Ks+X8E5vjv0z7c+jMc 3kxPYE7ju1ReMKvTbhoEJBnwFtuaaNU8/DVnsK5rlWqUaezhST8PsgSI4djWJk7tjaSN OiB+CXU4PN2xIPnuJ20vJ1BWJM7J7XYhX+O5jibdysvPfuSkVNU6D42ZWIako4Gu8DMO 97i6Oi2xFr3yojKAziUQximRKE95intqqAmRu2zE25vUe5l09Jc4by8LvFyhuP5kIMeC lX9Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=to:subject:message-id:date:from:in-reply-to:references:mime-version :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=ASB97v3WCIvNTcIVuNopzS7yi91aNSltn3kSdXe7kxo=; b=iOS5kGADNRhpXvbunaMy5s4o/BEALOFmjFcsZUMYIPQ4oed0f4FpyNG6czV1CHCPsB GOxdvHqotOZRHpIUSEbolXMH7EL9QQ3qVCKFIG5xCZ4k/QjhCISQ+liIjNoRVzVlYnHZ YQBM20G/qGD/jntBtiqgAHMmAuQBTlgrS4c2PjlDYCf8rNMErpRdEAp/MXpngTmVzZGN 5Po/umvyyFpbE6KmfHmQwAuXNYdEPRxRz5MCPoDg0FepItoPqtnw+waAKH46enycjCw9 rrHsiJ6KVTJ7alZ4Kvv9xYvxfvb4jHYcZ9pwm1oBXow20/qIDV7fXCUcbmses8TZLWZ0 5hlg== X-Gm-Message-State: ANoB5pkOVvVTpzPSQv31Fer0A3pImydODFJy1YN8yp00a6n0TcGRgr8r k/GQxJPscNZ1YvbE8wiATGSD7ixFJKyn/J9gNKHJ7VYB3SQ= X-Google-Smtp-Source: AA0mqf5l5rdZl/s5e25q7vcb23UZSNHz7XcJJuN6BAnglqQPvEB62xw9fSmQv9nnnJYTB3nN4B0NUCsF99XqlSIhFls= X-Received: by 2002:a05:600c:1e8c:b0:3d1:221d:51e6 with SMTP id be12-20020a05600c1e8c00b003d1221d51e6mr39827wmb.4.1670974056196; Tue, 13 Dec 2022 15:27:36 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: Date: Tue, 13 Dec 2022 23:27:25 +0000 Message-ID: To: internals@lists.php.net Content-Type: multipart/alternative; boundary="0000000000000c6cda05efbdf77c" Subject: Re: [PHP-DEV] Revisiting RFC: Engine Warnings -- Undefined array index From: davidgebler@gmail.com (David Gebler) --0000000000000c6cda05efbdf77c Content-Type: text/plain; charset="UTF-8" On Mon, Dec 12, 2022 at 10:20 PM Dan Liebner wrote: > As the RFC points out, "Some languages, such as JavaScript, do not consider > accesses to undefined array keys to be an error condition at all, and allow > such an operation to be performed silently." > It is by definition an error condition though. You're trying to access something which doesn't exist. > > For the last 20 years, it's been my impression that PHP also embraced this > philosophy, and that raising an E_NOTICE for "Undefined index" was more or > less offered as a debugging tool, as a matter of practicality. > In the earlier days of PHP, it was more acceptable to allow garbage in scripts. Generally, the community consensus has been that trending away from that has been a positive. It's strongly arguable that E_NOTICE was never appropriate for undefined array index access, since it is not really something which can occur in "the normal course of running a script" (since it necessarily means your script is broken). > > JavaScript returns the undefined primitive for accesses to undefined array > and object keys. PHP returns the NULL primitive respectively despite > raising an error. > > Here's my biggest criticism: the change essentially forces this: > $arr['key'] > > to be rewritten as this (or some other intolerably bad alternative): > isset($arr) && isset($arr['key']) && $arr['key'] > > This is a major regression in my opinion as far as productivity and > readability are concerned. > It seems strange to quibble about readability and syntax when you're talking about a situation in your code which is by definition an error condition. Accessing something which doesn't exist isn't the same as accessing something which does exist and it not having a value (i.e. the value is a null). Null = "Can I have the address for this customer please?", "No sorry, we don't have an address on file for them. "Undefined = "Can I have the gooble-gabble for this customer please?", "The what?" But sure, we don't always have a guarantee about the structure of some data, especially in web where input usually comes from an HTTP request which may or may not contain anything. But in those situations we can check instead of making assumptions. Good = "Hey, just checking, do we have something called gooble-gabble on file for this customer?", "Sure, here it is" for one customer, "No, sorry that's not a thing" for another (this is your array_key_exists / isset / null coalesce / whatever) Bad = "Can I have the gooble-gabble for this customer please?", "Sure, here it is" for one customer, "The what?" for another > > It has been proposed to make the error level of "Undefined index" > configurable so that teams and individual developers can decide for > themselves how they want this situation to be handled. There's a much better way of allowing developers to decide for themselves how to handle the gooble-gabble situation in a future major release; have undefined index access throw an UndefinedIndexError which can be caught. Then we can say "Can I have the gooble-gabble for this customer please? And if gooble-gabble isn't a thing, no problem, I can deal with that" That's my two cents, cheers. --0000000000000c6cda05efbdf77c--