Re: [RFC] [Discussion] array_first() and array_last()

From: Date: Tue, 08 Apr 2025 22:26:31 +0000
Subject: Re: [RFC] [Discussion] array_first() and array_last()
References: 1 2  Groups: php.internals 
Request: Send a blank email to [email protected] to get a copy of this message
On 4/8/25 8:46 PM, Claude Pache wrote:
> Hi,
> 
> I think that this argument is not convincing, and even counterproductive:
> 
> * NULL is a rare legitimate value, so the potential for clashing is low
> 
> First, it says that it is a “rare” legitimate value, which one can disagree with. (I do
> disagree.)
> 

I think it's a matter of phrasing, but I agree that it can be more nuanced.

> Second, the way it is formulated, it implies that, when null is used in array,
> there will be “clashing”, which is not necessarily the case. (I consider that there is almost
> never a clash, because it is rarely useful to make the difference between an explicit
> null and a missing value, and, when it is useful, you have almost surely already
> checked for an empty array upfront.)
> 
> I suggest to replace that argument with the two following ones, that doesn’t treat arrays
> with NULL as second class citizens:
> 
> * Semantically, NULL represents a missing value. Returning NULL from an empty array is not
> semantically incorrect, but it means that the function doesn’t differentiate between a implicit
> missing value (empty array) and an explicit missing value (array with NULL as its first/last
> element).
> 
> * In the relatively rare cases you do want to make the difference between an empty array and an
> array that starts/ends with NULL, you can (and should) just check for empty array upfront.
> 

That's indeed a bit better worded and more nuanced. I might adapt the RFC text a bit; although
in the end it kinda means the same thing anyway.

> -------
> 
> One more thing. On https://www.php.net/, I read: “Fast,
> flexible and pragmatic, PHP powers everything from your blog to the most popular websites in the
> world.” (emphasis added). If we were to design some new perfect language, we might consider making
> array_first() (or equivalent) choke on empty arrays. But given the current state of the
> affairs, the pragmatic thing to do is to pave the cowpath.

I agree. I value consistency; we have too much inconsistency already in PHP anyway, let's not
add more (unless we would have a very very good reason to do so; but that's not in this case
IMO).


Thread (22 messages)

« previous php.internals (#127080) next »