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

From: Date: Wed, 18 Oct 2023 02:11:03 +0000
Subject: Re: Two new functions array_first() and array_last()
References: 1  Groups: php.internals 
Request: Send a blank email to internals+get-121366@lists.php.net to get a copy of this message
Since reset() and end() return false when the array is empty, in a sense, there is an idea that there is no need to take such strict care of the return value. If you were to take proper care, you would probably specify a default value or throw an exception, as has already been suggested. However, specifying a default value is not very smart in my opinion. The fact that what value is considered to be "unable to obtain" changes depending on the situation causes some complexity. And when dealing with an array whose values ​​are completely unknown, it becomes necessary to "hope" that the values ​​do not conflict with the default values. Can the following signature meet your requirements? Or will it become too complicated? ``` // arrar_first(array $arr, bool &$has_value) $value = array_first($arr, $has_value); if ($has_value) { // array has items // $value is mixed } else { // array is empty // $value === null } ``` Regards. Saki

« previous php.internals (#121366) next »