PHP 8.5: new features that will make your code clearer, more secure, and easier to maintain

Abstract and modern representation of PHP 8.5. The logo “PHP 8.5” in vibrant colors, placed on a dark background with stylized lines of code or a digital data grid, symbolizing speed, performance, and technology.

PHP 8.5 arrived on November 20, bringing improvements that enhance the development experience, code readability, and debugging capabilities. It is an update designed to solve practical problems: less repetitive code, more control over errors, and better support for modern environments and multilingual applications.

In this entry, we'll tell you about the most important new features and how they impact daily work on PHP projects.

1. New URI extension: native URL handling

PHP 8.5 incorporates a built-in extension for parsing, normalizing, and manipulating URLs according to the RFC 3986 and WHATWG URL standards. This improvement reduces dependence on external libraries and avoids common errors when handling paths or parameters. It also provides greater consistency when working with applications that consume APIs, simplifying URL processing in complex projects.

2. Pipe operator (|>): a more expressive and functional code

One of the most eagerly awaited new features is the |> operator, which allows functions to be chained together and values to be passed between them without using intermediate variables. The result is cleaner and more readable code, especially in transformation pipelines.

$result = $data
    |> sanitize(...)
    |> validate(...)
    |> transform(...);

Although it has some limitations (such as requiring callables with a single mandatory parameter and not accepting parameters by reference), its impact on clarity is significant. This new syntax favors a more functional and fluid programming style, especially in projects that process data in several stages.

3. New array_first() y array_last() functions

PHP 8.5 introduces array_first() and array_last(), two simple but highly anticipated functions for obtaining the first and last values of an array, even if it is associative. If the array is empty, they return null. This avoids writing repetitive logic and makes the intent of the code more explicit, allowing for clearer and more concise operations when working with data structures.

4. Clone with: copy objects and update properties easily

The update adds an extension to the use of clone() that allows you to clone an object and modify properties in the same expression. This feature is especially useful for classes with immutable or readonly, simplifying patterns that previously required several steps and helping to keep code more declarative and tidy.

5. #[\NoDiscard] attribute: goodbye to silent errors

This attribute generates a warning if the return value of a function marked as critical is ignored. This helps prevent errors that can go unnoticed, especially in methods that affect the state of the application or validate sensitive information. It also reinforces good development practices and improves the clarity of the program flow.

6. Improvements in debugging and error handling

One of the most valuable improvements for everyday use is that fatal errors now display a complete stack trace, facilitating diagnosis even in environments where it was previously difficult to identify the root cause. Added to this are two new functions: get_error_handler() and get_exception_handler(), which allow you to inspect currently active handlers. This capability is especially useful for frameworks, libraries, or systems that temporarily modify the error handling configuration.

7. Better support for localization and RTL languages

PHP 8.5 incorporates functions such as locale_is_right_to_left() and Locale::isRightToLeft() to detect whether a language is written from right to left, such as Arabic or Hebrew. This allows interfaces, CSS classes, and visual behaviors to be adjusted more precisely. For multilingual applications, this improvement represents an important step toward more complete internationalization.

8. cURL extension: more efficient handling of multiple sessions

The new curl_multi_get_handles() feature allows you to retrieve all handles associated with a multi-handle, making it easier to manage multiple concurrent requests. In addition, cURL handles can now persist between PHP requests, reducing initialization costs and improving performance in applications that frequently interact with the same hosts.

9. More powerful CLI: php --ini=diff

The console incorporates the php --ini=diff option, which displays only the INI directives that have been modified from the standard configuration. This feature simplifies comparison between environments and allows for faster configuration auditing, especially on computers that handle multiple development environments.

10. PHP_BUILD_DATE constant

The new PHP_BUILD_DATE constant allows you to know the compilation date of the PHP binary, which is useful for audits, version verification, and diagnostics in environments where multiple builds of the language coexist.

11. Closures and callables in constant expressions

PHP now allows static closures and first-class callables to be used within const expressions, even as attribute parameters. This expands the language's configuration possibilities, making the code more declarative and allowing complex behaviors to be defined at compile time.

12. Performance and security improvements

The version also brings significant internal optimizations. BCMath now uses a stack instead of a heap, which improves performance. XMLWriter receives adjustments that reduce resource consumption. In addition, new opcache directives (such as opcache.file_cache_read_only) help optimize cache behavior. Added to this are critical bug fixes, such as memory leaks, overflows, and UAF cases, which strengthen the stability of the language.

A release focused on practicality

PHP 8.5 does not transform the language from the ground up, but it does offer practical improvements that make development cleaner, more expressive, safer, and more efficient. For teams looking to maintain modern codebases or prepare for the jump to PHP 9.0, this release represents a solid and timely step forward.