Re: Two new functions array_first() and array_last()

From: Date: Tue, 17 Oct 2023 09:10:18 +0000
Subject: Re: Two new functions array_first() and array_last()
References: 1  Groups: php.internals 
Request: Send a blank email to internals+get-121341@lists.php.net to get a copy of this message
> 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 = \array_key_first($array); if ($key === null) { // handle the failure } else { // success $value = $array[$key]; } ``` Yes, it would be slightly nicer if we could do: ```php $value = \array_first($array); if ($value === 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.

« previous php.internals (#121341) next »