PHP 8.5 introduces several new helper functions to simplify everyday coding tasks, improve readability, and provide better control over localization and error handling. Let's take a look at each of them.
1. array_first() and array_last()
Working with arrays in PHP often requires manually fetching the first or last element using functions like reset() or end(). These approaches can be verbose or modify the array's internal pointer, leading to unexpected behavior.
PHP 8.5 simplifies this with two new functions:
$array = ['apple', 'banana', 'cherry'];
echo array_first($array); // apple
echo array_last($array); // cherry
Both functions are non-destructive — they do not change the internal pointer of the array and work consistently with both numeric and associative arrays.
$fruits = ['a' => 'apple', 'b' => 'banana'];
echo array_first($fruits); // apple
echo array_last($fruits); // banana
These are particularly useful for functional or data-driven code that chains or applies array operations dynamically.
2. locale_is_right_to_left()
For developers building multilingual applications, text direction is an important consideration. PHP 8.5 adds a new function, locale_is_right_to_left(), along with its object-oriented counterpart Locale::isRightToLeft().
This function determines whether a given locale uses a right-to-left (RTL) writing direction, which is helpful for dynamically adjusting layouts, aligning text, or applying styles.
var_dump(locale_is_right_to_left('en_US')); // bool(false)
var_dump(locale_is_right_to_left('ar_EG')); // bool(true)
It enhances PHP's internationalization (Intl) capabilities, especially in applications that support both LTR and RTL languages.
3. get_exception_handler() and get_error_handler()
PHP has long provided set_exception_handler() and set_error_handler() to customize how exceptions and errors are handled. However, there was no built-in way to retrieve the currently active handlers — until now.
PHP 8.5 introduces get_exception_handler() and get_error_handler(), giving developers visibility and control over their current error/exception handling setup.
set_error_handler('customErrorHandler');
set_exception_handler('customExceptionHandler');
var_dump(get_error_handler()); // string(19) "customErrorHandler"
var_dump(get_exception_handler()); // string(23) "customExceptionHandler"
These functions are handy for frameworks, libraries, and debugging tools that need to inspect or temporarily override global handlers safely.
Why These Matter
Together, these helper functions make PHP 8.5 code cleaner, more intuitive, and more powerful:
- Simpler array handling with
array_first() and array_last().
- Smarter localization with
locale_is_right_to_left().
- Improved debugging and flexibility with
get_error_handler() and get_exception_handler().
They may seem small individually, but collectively, they enhance developer productivity and make modern PHP code more expressive.