Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:121342 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 95995 invoked from network); 17 Oct 2023 09:15:50 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 17 Oct 2023 09:15:50 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 3C931180087 for ; Tue, 17 Oct 2023 02:15:49 -0700 (PDT) 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, 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-oi1-f177.google.com (mail-oi1-f177.google.com [209.85.167.177]) (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, 17 Oct 2023 02:15:48 -0700 (PDT) Received: by mail-oi1-f177.google.com with SMTP id 5614622812f47-3b2e07e5f3aso45716b6e.0 for ; Tue, 17 Oct 2023 02:15:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1697534148; x=1698138948; darn=lists.php.net; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=rLBLwWlchvpTAVpv+YzPz+FJYz2VMITWFeF8HKN5N90=; b=DoMdpMS7ldhqu/W51dxC7KWP3VMYtrgjJJ2+C2rYAxKJZdwv1r/WTBNOoa/uNKDZIi IzLbfJP98COdveMQ80DOLd2F15dncP3r2ACzDbyWvZwrHY6SS9jzfN1SATV9ue2ujn0j NEaqpPUD0Y5HeNQyiP1lsmbwidOlv53oX5i8+LqjrdXcaitusXmKj3vytqWcy3R3Fb9r o26pQagPMMb/pk15KK5QeaqSt99n+p8j5kjk4DEkvaCvS9xt+5Y+zaBaS+WeDbHN3nym WXx06JBQjAaZUxcWgj+uVDj0QZeGNzL6GuoSFhIPRpEOe0NXnwr3RVjXuN11ejmBeBXp oDAQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1697534148; x=1698138948; h=content-transfer-encoding:cc: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=rLBLwWlchvpTAVpv+YzPz+FJYz2VMITWFeF8HKN5N90=; b=lshJ6leUeZWYfMXloUSzPbGLGJgdCr1EChptu+usPOqWSwpU4Poy5xOW/bFm5MZJUd B52BiZ+Ol8DcPehqN5nxqLUWcyvh366StZDW4Uj77s0wfxQT2/wjIh4Q8RjIJkKGDeav LR7UwIWTs8if4wNt6wE6nrn9LuTbq6MKHhYTKl/MUDT7tqH2F0GJqL4gjsgeyYMXllIx s8ijFRk97P07lc9ji/xv3CfBXXojor/T638Jpkg1p71TxXuUlRl7pjs69RqdhOHAyiPC WNiMn06wp6MtcZ4B2hd6cbhKk71ZnT3GtNrltTwyDdwoH1tve80UjfOWgdYdK2MozKzK y31g== X-Gm-Message-State: AOJu0Yy+uZLv4zS1GRuO+k8we6hF7fB9ONUk5+QSLIugzNXt133GFpRU fGy9SFED//iVYbIDXC9SlOoUh4YAfcCuoJeOQLc= X-Google-Smtp-Source: AGHT+IEjgTH9+7mqAVLhuerhdAi3qY6XeSWKduJJG81PLGnXFIlHiP1PjqDDf4lKwjqMFoNjRcE/oP8AZqoo1LyRnnE= X-Received: by 2002:a05:6808:624b:b0:3af:6ce3:ff0b with SMTP id dt11-20020a056808624b00b003af6ce3ff0bmr624572oib.19.1697534147800; Tue, 17 Oct 2023 02:15:47 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: Date: Tue, 17 Oct 2023 11:15:36 +0200 Message-ID: To: Levi Morrison Cc: David Grudl , internals@lists.php.net Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Subject: Re: [PHP-DEV] Two new functions array_first() and array_last() From: landers.robert@gmail.com (Robert Landers) On Tue, Oct 17, 2023 at 11:10=E2=80=AFAM Levi Morrison via internals wrote: > > > c) Two such functions were proposed and rejected during the > > array_key_first/last RFC > > (https://wiki.php.net/rfc/array_key_first_last) > > > > Yes, that was in 2018. At that time, functions like str_contains() or > > str_starts_with() wouldn't have even come into existence, just because > > there was an obscure way to do it without them. I believe we've moved > > on since then. Today we know how useful it is to use simple, > > easy-to-understand methods, both for programmers who write and read > > the code. > > It's true that sentiment may have shifted in this time. However, a > common argument at that time still stands: `null` is not a good > sentintenal for failure because the value inside the array very well > could have been null. This is not true for the keys. For me > personally, I think I would still vote no. I'm not entirely sure about > that, but that's how I would lean right now. > > As it stands, you'd have to write code along the lines of: > > ```php > $key =3D \array_key_first($array); > if ($key =3D=3D=3D null) { > // handle the failure > } else { > // success > $value =3D $array[$key]; > } > ``` > > Yes, it would be slightly nicer if we could do: > > ```php > $value =3D \array_first($array); > if ($value =3D=3D=3D null) { > // handle the failure > } else { > // success > } > ``` > > But I fear in practice people will just omit the error checking. > > One way around that is to throw an exception. I'm not sure how I feel > about that, but I'll think about it. > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: https://www.php.net/unsub.php > How is returning null any different than $array[0] on an empty array? https://3v4l.org/K3gRs Currently, it just outputs a warning, but the result is `null`.