[php-src] Issue #12435: Nové dvě funkce array_first() a array_last()
| From: | dg | Date: | Sat, 14 Oct 2023 11:34:01 +0000 |
| Subject: | [php-src] Issue #12435: Nové dvě funkce array_first() a array_last() | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-245566@lists.php.net to get a copy of this message | ||
Issue: https://github.com/php/php-src/issues/12435
Author: dg
### Description
PHP lacks two very basic functions for working with arrays:
-
array_first() returning the first element of an array (or null)
- array_last() returning the last element of the array (or null)
While PHP has functions that return the first and last keys (array_key_first() and
array_key_last()), it does not have more useful functions for values.
Thus, programmers "abuse" the reset() and end() functions for
this purpose. The problem is that:
- these functions are used to move the internal pointer in the array
- which is why they have a name that is inappropriate when used in the sense of "return me the
first element"
- have a side effect (i.e. moving the pointer)
- in the absence of an element, they return the obsolete false and not the currently expected null,
which can be combined with the ?? operator
- in this they differ from the similar functions array_key_first() and
array_key_last()