PHP 8.5: Top New Features You Must Know with Code Examples

logo icon

Admin Teguh

-

2025 August 21

iamge article

PHP 8.5, which is scheduled for stable release on November 20, 2025, brings significant improvements for PHP developers with a focus on readable code, efficiency, and ease of debugging. In this article, we will explore some of the key features of PHP 8.5 in detail, including new array functions or methods such as array_first and array_last, the pipe operator (|>), support for closures and first-class callables in constant expressions, as well as the new functions get_error_handler and get_exception_handler.

Top Features in PHP v8.5

PHP 8.5 comes with a range of new features designed to improve the way we, especially PHP developers write code. Here are the key things every PHP developer should know.

New Array Functions

The array_first and array_last features in PHP 8.5 allow developers to retrieve the first or last element of an array in a simpler and safer way, without changing the internal array pointer. This addresses the limitations of older methods such as reset() or end(), which can cause issues with array iterators.

Implementation of the array_first() and array_last() functions

Both functions work on associative and numeric arrays, and automatically dereference if the element is a reference.

// Examples with array numeric
$fruits = ['apple', 'banana', 'orange'];
echo array_first($fruits); // 'apple'
echo array_last($bfruits);  // 'orange'

// Examples with array associative
$data = ['name' => 'John Dev', 'age' => 25, 'City' => 'New York'];
echo array_first($data); // 'John Dev'
echo array_last($data);  // 'New York'

// Empty Array
$emptyArray = [];
echo array_first($emptyArray) ?? 'Array is Empty';
echo array_last($emptyArray) ?? 'Array is Empty';

// Array with random key orders
$randomArray = [1 => 'a', 0 => 'b', 3 => 'c', 2 => 'd'];
echo array_first($randomArray); // 'a'
echo array_last($randomArray);  // 'd'

Summary of the array_first() and array_last() functions

  • array_first(array $array): mixed: Returns the first value of the array based on the key order (not the numerical index). If the array is empty, it returns null.
  • array_last(array $array): mixed: Returns the last value of the array. Like array_first(), it returns null for an empty array.
  • This feature is consistent with functions like array_key_first() and array_key_last() introduced in PHP 7.3, but it directly returns the value, not the key.
  • Reduces boilerplate code and avoids iterator modifications, which can cause bugs in complex code. And returns null for empty arrays, making it easier to handle with the null coalescing operator (??).

Pipe Operator

The pipe operator (|>) is one of the most anticipated features in PHP 8.5, allowing linear chaining of functions to improve readable code. This operator passes the value from the left to the function on the right as the first parameter, similar to functional programming in languages such as Elixir.

Implementation of Pipe Operator

// without Pipe Operator
$result = trim(str_shuffle(strtoupper('Hello World')));

// with Pipe Operator
$result = 'Hello World'
    |> strtoupper(...)
    |> str_shuffle(...)
    |> trim(...);

// mixing callable example
$result = 'Hello World'
    |> 'strtoupper'  // String callable
    |> fn($x) => trim($x)  // Arrow function
    |> new MyTrimmer()  // __invoke class
    |> [Trimmer::class, 'trimStatic'];  // Static method

// Class
class MyTrimmer {
    public function __invoke($str) { return trim($str); }
}

class Trimmer {
    public static function trimStatic($str) { return trim($str, ' '); }
}

Summary of Pipe Operator Explanation

  • Syntax: value |> function(...), where ... indicates a first-class callable. The operator evaluates from left to right and can be chained multiple times.
  • Each function must have at least one parameter (or can be without parameters, but will be ignored). By-reference parameters are not allowed, except in certain built-in functions.
  • Type coercion follows PHP rules; if strict_types=1, it may trigger a TypeError.
  • Code is optimized during compilation into opcode similar to nested structures, with copy-on-write memory efficiency.

Support for Closures and First-Class Callables in Constant Expressions

PHP 8.5 introduces support for static closures and first-class callables as default values in constants, class properties, and function arguments. This allows for more expressive code without the need for nullable type hints for optional callbacks.

Implementation of Closure Support and First-Callables on Constant Expressions

// constant class
class Config {
    const DEFAULT_FILTER = static fn($value) => trim(strip_tags($value));
}

// property class
class Processor {
    private array $callbacks = [
        static fn($val) => strtoupper($val),
        Processor::sanitize(...)
    ];

    private static function sanitize($val) { return htmlspecialchars($val); }
}

function process(string $input, callable $filter = static fn($x) => $x) {
    return $filter($input);
}

echo process('Hello'); // 'Hello'
echo Config::DEFAULT_FILTER('<b>World</b>'); // 'World'

Summary of Support for Closures and First-Callables in Constant Expressions

  • Supported Contexts: Can be used in global or class constants, class properties (static/non-static), and default function arguments.
  • Only static closures with static fn and static methods are supported, as constants must be evaluated at compile time.
  • Non-static closures are not supported, as they depend on $this or instance variables.
  • Reduces the need for null as the default callback, making the code safer and cleaner.

get_error_handler() and get_exception_handler()

These two new functions in PHP 8.5 allow direct access to active error and exception handlers, facilitating debugging, and dynamic error management.

Implementation of get_error_handler() and get_exception_handler()

// set handler
set_error_handler(function($errno, $errstr) { echo "Error: $errstr"; });
set_exception_handler(function($e) { echo "Exception: " . $e->getMessage(); });

// get handler
var_dump(get_error_handler());
var_dump(get_exception_handler());

// examples
if (get_error_handler() === null) {
    echo "no error handler.";
}

Summary of get_error_handler() and get_exception_handler()

  • get_error_handler(): ?callable: Returns the current error handler from the set_error_handler() function, or null if none exists.
  • get_exception_handler(): ?callable: Returns the current exception handler from the set_exception_handler() function, or null if none exists.
  • Allows inspection of handlers without modification, useful for libraries or frameworks that need to check error configurations.

Conclusion

The new features in PHP 8.5 bring new efficiencies for PHP Developers. With the detailed implementation of several PHP 8.5 code examples above, you can start trying out this beta version for your latest projects.

Thank you.

Teguh Budi Laksono

Teguh Budi Laksono

"not a perfect person, just someone who keeps learning and trying, because he believes that big dreams are born from a long and consistent process."

Tags :

php

php 8.5

top php 8.5 features you need to know

php developer

a new version php 8.5 will be released soon

web

web developer

learn coding with nganggurdev

Like the articles on NganggurDev? Let's give support to the writer. Thank you very much!

Want to read more? Click me!