PHP 8.5 introduces the pipe operator, URI extension, and functions array_first() and array_last(), making everyday programming easier. This version improves code readability, URL handling, and debugging, while offering performance optimizations and deprecations of unnecessary features. With these changes, you can write simpler and faster code for your websites.
Table of Contents
New Features and Improvements
Pipe Operator |>
The pipe operator |>, long-awaited by PHP developers, enables chaining function calls from left to right without intermediate variables. Instead of nested calls like strtolower(str_replace(‘ ‘, ‘-‘, trim($title))), you can simply write $title |> trim($$) |> str_replace(‘ ‘, ‘-‘, $$) |> strtolower($$). This makes code more readable and maintainable, especially for complex data operations.
URI Extension
PHP 8.5 adds a built-in URI extension for parsing and normalizing URLs compliant with RFC 3986 and WHATWG URL standards. You can now safely handle links without extra libraries, for example, using the Uri\Rfc3986\Uri class to extract the host from ‘https://php.net’. This greatly simplifies URL work in web applications and reduces error risks.
array_first() and array_last() Functions
The new array_first() and array_last() functions let you quickly retrieve the first or last array element, returning null if the array is empty. Instead of combining array_key_last() or end(), just use array_last($events). These simple additions save time and organize code when working with data lists.
#[\\NoDiscard] Attribute
The #[\\NoDiscard] attribute generates a warning when you ignore a function’s return value, helping avoid API mistakes. For example, a getPhpVersion() function with this attribute will trigger a warning if you don’t use its result, unless you explicitly use (void)getPhpVersion(). This tool improves code quality and security.
Cloning with Property Modification
You can now clone an object and immediately modify selected properties by passing an associative array to clone(), like clone($this, [‘alpha’ => $alpha]). This solution is perfect for readonly classes and simplifies the “with-er” pattern without manual data copying. Previously, it required extra logic in methods like withAlpha().
get_error_handler() and get_exception_handler() Functions
The get_error_handler() and get_exception_handler() functions allow checking current error and exception handlers during program execution. They facilitate debugging and understanding app behavior. They also support stack traces for fatal errors, speeding up problem resolution.
Other Useful Additions
PHP 8.5 adds constants PHP_BUILD_DATE and PHP_BUILD_PROVIDER for build date checks, the CLI option php –ini=diff to show INI changes, and locale_is_right_to_left() for right-to-left languages like Arabic. The curl_multi_get_handles() function helps manage cURL sessions, and persistent cURL handles speed up initialization.
Deprecations and Fixes
PHP 8.5 ends support for old casts like (boolean) or (integer) – use (bool) and (int) instead. Functions like curl_close(), xml_parser_free(), and socket_set_timeout() are deprecated as unnecessary. The disable_classes INI directive is removed, and switch case statements now require a colon. These changes clean up the language and prepare for future versions.