PHP 8.6 is out, and here’s what changes for your code

PHP 8.6 has been released. The new version adds partial function application, a Time\Duration class, a polling API for event loops and a clamp() function.

PHP 8.6

PHP 8.6 also changes defaults that existing apps could trip over. Sessions now start with session.use_strict_mode and session.cookie_httponly turned on and session.cookie_samesite set to Lax. Custom session handlers that don’t implement both create_sid() and validateId() are deprecated. If your app relied on the old defaults, test your login flows before you upgrade.

Failed writes now report failure

fclose(), file_put_contents() and copy() return false when flushing or closing a file fails. Earlier versions ignored that failure, so a write that broke at the last step could still look like it succeeded. Many other functions now throw a ValueError on out-of-range values, invalid flags, or strings that contain NUL bytes. You can also set stream operations to throw a StreamException. The exception carries structured error objects that name the wrapper and the path or URL involved.

What you can write now

Partial function application lets you fill in some of a function’s arguments ahead of time. You get back a closure that waits for the rest, and a ? marks each argument still to come:

$slugify = str_replace(' ', '-', ?);

echo $slugify('php 8.6 release'); // php-8.6-release

Time\Duration measures elapsed time the way a stopwatch does, with no clock time or timezone attached. It covers more than 250 years at nanosecond precision, which is more than any retry delay or timeout will need. Io\Poll watches many streams at once through epoll, kqueue, event ports or WSAPoll, without the limits of stream_select(). PHP’s developers built it as a base for event-loop libraries such as ReactPHP and AMPHP. And clamp(110, 0, 100) returns 100.

The release also adds a SortDirection enum, default values for readonly properties, and post-quantum key encapsulation in Sodium (X-Wing and ML-KEM-768). TLS streams gain support for TLS 1.3 early data.

What’s on the way out

The mbstring regex functions, including mb_ereg() and mb_split(), are deprecated because Oniguruma, the regex library behind them, is no longer maintained. PHP’s developers recommend PCRE with the u modifier instead. is_double(), is_long(), is_integer() and doubleval() are deprecated in favor of is_float(), is_int() and floatval(), and spl_object_hash() is deprecated in favor of spl_object_id(). Using return inside a finally block is deprecated too. One behavior change applies right away: trim(), ltrim() and rtrim() now strip form feeds (\f) by default.

More about

Don't miss