Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:121344 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 99173 invoked from network); 17 Oct 2023 09:30:05 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 17 Oct 2023 09:30:05 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 3B568180504 for ; Tue, 17 Oct 2023 02:30:04 -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_H3,RCVD_IN_MSPIKE_WL,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-ot1-f47.google.com (mail-ot1-f47.google.com [209.85.210.47]) (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:30:03 -0700 (PDT) Received: by mail-ot1-f47.google.com with SMTP id 46e09a7af769-6ccfed5e05bso81893a34.1 for ; Tue, 17 Oct 2023 02:30:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1697535003; x=1698139803; 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=SNLM0DYxunUMHjY3U4bz4rlAYPPMUKGpj8otb0a75Mg=; b=kuBu5s8HYNcw5UEdezMnXe+gcvCy98hhOViuU/zh/IWePN/1cIdc23ggc3Gt4FTshP W1uShy7GBO058895otMUxHJ8VRIpd1pXqABQ0RZJUay+FcS1XnyjD7UIWmlYKdgk0sKh XP9IGl9h+N7NAuxOXnUyublcRt1rBsC9Lnh02j7MMCGCVAYc51BIiVNf2tv19RoJFpQq 5UJmyjx+/q+o6zzHxlFHldbY0FZMeILY01Mb2MgdYJrMtgXybsEFm5G38CSykNyn6bKy b7DA78sCzSF60kNg77BX02b063BY2EMGKyKU8GXYCrPsP9ioyB5N4n9wNeRYu7t7EQcf dAQw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1697535003; x=1698139803; 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=SNLM0DYxunUMHjY3U4bz4rlAYPPMUKGpj8otb0a75Mg=; b=lGI++9yD600EnH5+JX74+jsYHdY+VnddLU1k/uySyFtJnQtm3xmRTOruPjfX9IE0F9 YwV63kBXDjSt5GoJRKDxbK3FoLfCd5N3IfwOFTXDLl+bFJn+OnGr1lnV3GlFHbP6ZKI3 bTp6pbciXorQXB7mZTBqxq9KS5s4Re8tbEZFcexdmXZY6Sfssh0SQK6HzmHu9nwBcYb9 2uDhRxvbEe0vH1fGEWhDLe95hdKQffn57wcy9j5tz/6VsDeLZYdLjbTKH6fm8LgXieL4 nnsDzsoOMRnd3JO7gFtg1CuEYmjl4Srjl6uGklmGWBCjWOayk+v3duQjTVfFa/mpsFpd uWew== X-Gm-Message-State: AOJu0YyLkyXuOxRynjCokO3W42jQNP1///3+6KzmPV1WgZZBmLGJgF/9 aleFAG5Tag9SyzYIL8l8V+FSCxUleawsD/RV4WI= X-Google-Smtp-Source: AGHT+IGoMQ+ZiK6eF379b/jDSFEOTO3DVp6hhy5e0d+TATpx/ewHHFreBdo9ExHypJ4w9+EueWX+U1GfDYoKnXYCkds= X-Received: by 2002:a05:6830:1b7c:b0:6c4:fc6d:88fb with SMTP id d28-20020a0568301b7c00b006c4fc6d88fbmr1501634ote.33.1697535002995; Tue, 17 Oct 2023 02:30:02 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: Date: Tue, 17 Oct 2023 11:29:51 +0200 Message-ID: To: Lynn Cc: Levi Morrison , 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:22=E2=80=AFAM Lynn wrote: > > > > On Tue, Oct 17, 2023 at 11:15=E2=80=AFAM Robert Landers wrote: >> >> On Tue, Oct 17, 2023 at 11:10=E2=80=AFAM Levi Morrison via internals >> wrote: >> >> How is returning null any different than $array[0] on an empty array? >> https://3v4l.org/> > > > > 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 becaus= e > > > 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 >> >> >K3gRs >> >> Currently, it just outputs a warning, but the result is `null`. >> >> -- >> PHP Internals - PHP Runtime Development Mailing List >> To unsubscribe, visit: https://www.php.net/unsub.php >> > > I'm okay with `null` being given back as it's the current behavior of acc= essing the key as well, which is often checked through `$array[0] ?? null`.= Something like an `undefined` type could solve this problem, but personall= y not a fan of this in Javascript. Maybe `array_has_first` or something cou= ld be made, but honestly I would still just check for `null` myself most li= kely. Huh, that's a good observation Lynn. If I might suggest an API change to `array_value_first` (and a similar one for array_value_last), it would be this: `array_value_first(array $array, string|int|null &$key =3D null): mixed` where you also get back the key. If you want to know if null is an error or an actual value: $value =3D array_value_first($array, $key); if($key =3D=3D=3D null) // handle error else // do something with $value You can also freely ignore the key, if you don't need it or care about error checking. That would save doing two function calls on the same array, just to do some error checking.