#63853 closed enhancement (fixed)
Add PHP 8.5 compat array functions: `array_first` and `array_last`
| Reported by: | | Owned by: | |
|---|---|---|---|
| Milestone: | 6.9 | Priority: | normal |
| Severity: | normal | Version: | |
| Component: | General | Keywords: | php85 has-patch has-unit-tests commit has-dev-note |
| Focuses: | php-compatibility | Cc: |
Description
PHP 8.5, expected to be released on November 20, 2025, thus shortly before WP 6.9, will bring two new array functions: array_first and array_last.
See https://wiki.php.net/rfc/array_first_last
Following up on [59783] (for #62558), [57337], and [52038], we should include polyfills in Core, to make developer adoption easier. For more reasoning, see ticket:62558#comment:6 .
Further details as well as possible polyfills are available at https://php.watch/versions/8.5/array_first-array_last and in the polyfills/array-first-array-last package.
Change History (15)
#2
@
3 months ago
@tusharbharti Thank you, that would be very helpful.
If the values passed to the parameters are empty-ish like null, 0 , false etc, should the function return null ( by allowing empty-ish value to be passed ) or throw error when trying to access the key for these.
In PHP the new functions are type strict so the compatibility implementations should be too. If you define the functions as function array_*( array $array ) then that will trigger the fatal error.
If any error occurs while trying to get the first/last key of the array or trying to get the value from the array, should the function handle these errors or this is left for consumers to handle.
Being type strict, this should be a use case that needs to be covered.
This ticket was mentioned in PR #9556 on WordPress/wordpress-develop by @tusharbharti.
3 months ago #4
- Keywords has-patch has-unit-tests added; needs-patch removed
Add polyfills for array_first() and array_last(), planned for PHP 8.5. These provide a safe, side-effect-free way to fetch the first and last values of an array. This forward-compatibility makes it easier for Core, plugins, and themes to adopt upcoming PHP features while maintaining support for older versions.
The PR includes the polyfills for both functions in compat.php as well as unit tests.
Trac ticket: https://core.trac.wordpress.org/ticket/63853
@tusharbharti commented on PR #9556:
3 months ago #5
Hi, I have a question ( not related to ticket but related to tests). Is there a guidelines on how tests comments are to be written as when I checked the tests files for PHPUnit, some files have comments as well as ticket mentioned but others either don't have any comments nor the tickets.
@johnbillion commented on PR #9556:
3 months ago #6
Here's the PHP implementation for reference: https://github.com/php/php-src/blob/0bf295944df7171acf47502d31dd7df8b9155d81/ext/standard/array.c#L4528-L4552
@peterwilsoncc commented on PR #9556:
3 months ago #7
Is there a guidelines on how tests comments are to be written
It's a bit of mixed bag but something contributors are working toward improving. It's become relatively common for contribuitors to use the order listed in the description of this trac ticket.
@tusharbharti commented on PR #9556:
3 months ago #8
It's a bit of mixed bag but something contributors are working toward improving. It's become relatively common for contribuitors to use the order listed in the description of this trac ticket.
Oh, got it! 👍, I will follow this format from now on.
Is there anything else that I need to implement from my side on this?
#10
@
3 months ago
- Owner set to jorbin
- Status changed from new to assigned
Self assigning for final review
#12
@
3 months ago
In array_first() we could use
return $array[ array_key_first( $array ) ]; instead of
foreach ( $array as $value ) { return $value; } #13
@
3 months ago
@Soean: I think this was a deliberate choice to save a function call, see https://github.com/WordPress/wordpress-develop/pull/9556#discussion_r2289844485
It's the same functionality, minus a otherwise-duplicated empty() check, see https://github.com/WordPress/wordpress-develop/blob/6.8.2/src/wp-includes/compat.php#L310-L312
#14
@
3 months ago
PHP also does this internally:
ZEND_HASH_FOREACH_VAL(array, zval *zv) { RETURN_COPY_DEREF(zv); } ZEND_HASH_FOREACH_END(); #15
@
4 days ago
- Keywords has-dev-note added
These new polyfill functions were detailed in the miscellaneous dev note for developer-focused changed: https://make.wordpress.org/core/2025/11/17/miscellaneous-developer-focused-changes-in-6-9/
Hi, I am interested in creating a compat for
array_firstandarray_lastbut would like to confirm how the function should handle following cases: