On Sat, Apr 5, 2025 at 9:51 AM Niels Dossche <dossche.ni...@gmail.com> wrote:
>
> Hi internals
>
> I'm opening the discussion for the RFC "array_first() and array_last()".
> https://wiki.php.net/rfc/array_first_last
>
> Kind regards
> Niels

I dislike all the functions where `null` is a valid value, which can
also be confused with an error in the operation.

However, this is kind of in the grey area for me, because we do have
plenty of tools to avoid the error condition. Of course there's the
simple if:

    if (!empty($array) {
        $first = array_first($array);
    }

And if we have a default value that could be used:

    $first = !empty($array) ? array_first($array) : $default;

I would prefer a better design tool than returning null, but being
honest, neither of these are too bad. As long as our documentation for
these functions are helpful but concise about showing how to work
around these edges, then I may be convinced to vote yes despite the
design.

Reply via email to