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

From: Date: Wed, 18 Oct 2023 13:11:13 +0000
Subject: Re: Two new functions array_first() and array_last()
References: 1 2 3 4  Groups: php.internals 
Request: Send a blank email to internals+get-121390@lists.php.net to get a copy of this message
> This function signature can be accomplished by userland once we have > array_key_first() and array_first(). > It's much better to keep array_first() as simple as possible > and let everyone build their own approach to go about it since we have so many approaches. The goal wasn't necessarily to keep them together. The goal was to introduce an arguably simple way to: 1. Be able to get the first/last value of an array. 2. Know if the value was actually in existence or not, rather than just blindly returning null when it could be a valid value. 3. Accompany multiple use cases like needing to return a default if not found or throwing an exception. 4. Offer a way to know if the value existed or not and act accordingly without additional logic checking the key. 5. Address the seeming genericness of the function name. * If I were someone who seen the name for the first time. I'd see it and ask array_first what? And proceed to the docs. The different approaches are trying to figure out the best way to know whether the result was the actual value from the array or some baked in language default null. I get your desire to keep things simple, but IMO returning a value that does not conflict with possibly valid values or somehow indicates the value was not present is important, and should come before simplicity. Which likely means involving the key somehow.

« previous php.internals (#121390) next »