PHP 8.6 is out: what changes in your code, from clamp() to finally
PHP 8.6 adds partial function application, clamp() and defaults for readonly properties, deprecates return inside finally because it swallows exceptions, and makes sessions safer by default.

If you write PHP, you've probably written min(max($x, 0), 100) to keep a number between two limits, or put a return inside finally without thinking about it. PHP 8.6 is out with a fix for the first and a warning for the second. Here you'll see what's new that touches the code you write every day, and what to check in your project before you move.
What is PHP 8.6?
PHP 8.6 is the new major release of the PHP language, with new features in the code and a list of things that are now deprecated. Deprecated means the thing still works, but raises a notice because it will be removed in a later release.
According to the official PHP 8.6 release page, the main additions are Partial Function Application, a Duration class for time, a Polling API, and a clamp() function.
What does the clamp() function do?
The clamp() function takes a value and two limits, and returns the value itself if it sits between them, or the nearest limit if it falls outside. Instead of nesting min and max and mixing up their order, you write one function.
var_dump(clamp(-10, 0, 100));
// int(0)
var_dump(clamp(50, 0, 100));
// int(50)
var_dump(clamp(110, 0, 100));
// int(100)The release page says it works with any comparable value, not just numbers, including the new Duration class.
What is partial function application?
Partial function application means you give a function some of its arguments now, and get back a Closure that waits for the rest. You put ? where the argument you don't have yet goes.
$versions = ['8.4', '8.5', '8.6', '8.7', '9.0'];
$supportsPfa = array_filter(
$versions,
version_compare(?, '8.6', '>='),
);Before, you wrote fn($v) => version_compare($v, '8.6', '>='). Now the ? says "each value from the list goes here", and the rest stays fixed.
Why is return inside finally deprecated?
A return inside finally is deprecated because it swallows any exception thrown in the try or catch, and overrides the value they returned, without telling you. It's a silent bug that may have been in your code for years.
Take a function that saves an order to the database in try, and in finally closes the connection and does return true. If the save fails and throws, the return in finally wipes the exception, and the function returns true. The order wasn't saved, and the calling code thinks it was. The release page says a return in finally "silently discards exceptions", and that's why PHP 8.6 now raises a notice for it.
The fix is to keep finally for cleanup only (closing a file or a connection), and keep the return in try.
What else changes in your project?
A few small changes show up in your logs as soon as you run your project on PHP 8.6:
Sessions are safer by default:
session.use_strict_modeandsession.cookie_httponlyare on, andsession.cookie_samesiteisLax. These are the same settings from the session cookie flags article.Readonly properties can now declare a default value, so a fixed value no longer has to be assigned in the constructor.
trim(),ltrim()andrtrim()now also strip the form feed character (\f).fclose(),file_put_contents()andcopy()now returnfalsewhen flushing or closing the stream fails, instead of staying silent.mb_ereg()and the other mbstring regex functions are deprecated, and the release page suggests PCRE functions with theumodifier instead.
Where do you start now?
The lesson for you as a developer: the new notice about return in finally points you to a place that may have been hiding failures for a long time. Run your project on PHP 8.6 in a test environment, search the logs for every deprecation, and start with finally.
If your site is still on an older branch, first make sure it runs the latest security release of that branch; every security update is in the security updates topic. Then think about moving. All web development articles are in the web development topic. How many finally blocks in your code have a return?
FAQ
Should I move my project to PHP 8.6 now?
Not today. Try the project on it in a test environment first, read the deprecation notices in your logs, then move once the libraries you use support it.
Will old code with return inside finally stop working?
No. Deprecated means you get a deprecation notice and it keeps working. But the notice shows you code that may be hiding errors, so fix it.
What does safer sessions by default mean?
It means the safe session settings are on without you writing them: strict mode, an HttpOnly cookie, and SameSite set to Lax. If you already set them yourself, nothing changes.


