I created an easy to use API to help businesses do incredible things with AI.
PHP

An early look at PHP 9.0's new features and changes

Benjamin Crozat
Modified on Jan 1, 2024 6 comments Edit on GitHub
An early look at PHP 9.0's new features and changes

Introduction

PHP is an open-source project. Knowing what new features and changes are planned for the next version only takes a minute of research. For instance, this page lists all the accepted RFCs for different versions of PHP.

That being said, despite PHP 9.0 being planned, no work has been started yet and we have to dig deeper.

When will PHP 9.0 be released?

For now, the release date of PHP 9.0 hasn’t been announced yet. This version is still far in the future. We could get PHP 8.5 and 8.6 before 9.0 is even considered. Who knows?

How to install and test PHP 9.0?

To this day, no work has been started on PHP 9.0, so you won’t even be able to pull the latest code and compile it yourself.

New features and changes planned for PHP 9.0

Better increment and decrement behavior

PHP 9 is cleaning up how the ++ and -- operators work. Here’s what’s changing:

  1. No more weird string increments:

    // PHP 8 and earlier
    $foo = 'a9';
    $foo++;
    echo $foo;  // Outputs: 'b0'
    
    // PHP 9
    $foo = 'a9';
    $foo++;  // Throws a TypeError
    
  2. Booleans and null will be treated as numbers:

    // PHP 8 and earlier
    $bar = true;
    $bar++;
    var_dump($bar);  // Outputs: bool(true)
    
    // PHP 9
    $bar = true;
    $bar++;
    var_dump($bar);  // Outputs: int(2)
    
  3. Empty strings won’t magically become numbers:

    // PHP 8 and earlier
    $baz = '';
    $baz--;
    var_dump($baz);  // Outputs: int(-1)
    
    // PHP 9
    $baz = '';
    $baz--;  // Throws a TypeError
    

These changes make PHP more predictable.

If you still need the old string increment behavior, you can use the new str_increment() function.

PHP RFC: Path to Saner Increment/Decrement operators

PHP 9.0 throws an exception on unserialization errors

This RFC, Improve unserialize() error handling, which has been partially implemented in PHP 8.3, upgrades unserialization errors from E_NOTICE to E_WARNING.

In PHP 9.0, these will be upgraded to an UnserializationFailedException.

This will allow developers to stop using a custom error handler and get a behavior more consistent with other parts of the language.

// PHP 8.3: "Warning: unserialize(): Error at offset 0 of 3 bytes"
// PHP 9.0: "Uncaught UnserializationFailedException: unserialize(): Error at offset 0 of 3 bytes"
unserialize("foo");

Simplified function signatures

PHP 9 is will make functions easier to understand and use. How? By simplifying their signatures. Let’s break it down with two examples.

First, take a look at array_keys():

// Current PHP:
$allKeys = array_keys($myArray);
$specificKeys = array_keys($myArray, 'searchValue', true);

// PHP 9:
$allKeys = array_keys($myArray);
$specificKeys = array_keys_filter($myArray, 'searchValue', true);

See the difference? Instead of one function doing two jobs, we’ll have two separate, more focused functions.

Here’s another example with DatePeriod::__construct():

// Current PHP:
$period1 = new DatePeriod($start, $interval, $end);
$period2 = new DatePeriod('R4/2012-07-01T00:00:00Z/P7D');

// PHP 9:
$period1 = new DatePeriod($start, $interval, $end);
$period2 = DatePeriod::createFromISO8601String('R4/2012-07-01T00:00:00Z/P7D');

Again, we’re moving from one multi-purpose constructor to a constructor and a separate creation method.

Why these changes? They make PHP more predictable. When a function or method does just one thing, it’s easier to understand and use correctly.

Here’s the plan:

  1. PHP 8.3 introduces the new functions.
  2. PHP 8.4 warns you about using the old ways.
  3. PHP 9 (or 10, it’s still not decided) will complete the transition.

PHP RFC: Deprecate functions with overloaded signatures

This RFC proposes to deprecate and eventually remove autovivification (automatic creation of arrays) from false values in PHP. Here’s a simplified explanation for the blog post:

No more arrays out of false values

PHP 9 is getting stricter about how arrays are created, particularly when it comes to false values. Let’s break this down:

Currently in PHP, you can do something like this:

$arr = false;
$arr[] = 2; // This creates an array [2]

This feature, called “autovivification”, automatically converts false to an array. While convenient, it can lead to unexpected behavior and bugs.

In PHP 9, this won’t be allowed anymore. Instead, you’ll see an error:

$arr = false;
$arr[] = 2; // Error: Cannot use a scalar value as an array

You already can’t do this with other values like true or 0, so why should false be special?

PHP RFC: Deprecate autovivification on false

This RFC proposes to deprecate and eventually remove certain forms of string interpolation in PHP. Here’s a simplified explanation for the blog post:

Simplified string interpolation

PHP 9 is simplifying how you can embed variables in strings. Let’s break it down:

Currently, PHP allows several ways to put variables inside strings:

  1. Direct: "$foo"
  2. Braces outside: "{$foo}"
  3. Braces after dollar: "${foo}"
  4. Variable variables: "${expr}"

PHP 9 will keep options 1 and 2, but remove options 3 and 4. Why? Because they’re confusing and less useful.

For example, this won’t work in PHP 9:

$foo = 'world';
echo "Hello ${foo}";  // This will cause an error

Instead, you’ll need to use one of these:

echo "Hello $foo";     // Option 1
echo "Hello {$foo}";   // Option 2

PHP RFC: Deprecate ${} string interpolation

Some warnings will become errors in PHP 9.0

In order to make PHP more reliable, warnings for undefined variables and properties will now become errors.

For instance, the following code will not run anymore in PHP 9.0:

// PHP 8.x: "Warning: Undefined variable $foo"
// PHP 9.0: "Fatal error: Uncaught Error: Undefined variable '$foo'"
echo $foo;

Also, from what I understand, these changes with variables and properties will make the maintainers’ lives easier, which is good for everyone!

I let you check out the RFCs for more details:

Deprecated features from earlier PHP versions will be removed

Features that have been deprecated in PHP 8.1, 8.2, 8.3, and 8.4 (learn more about PHP 8.4) will finally be removed in PHP 9.0. This will translate to breaking changes for developers who ignored the warnings. 😅

Here’s a list of RFCs containing all the deprecated features:

6 comments

Tormi Talv
Tormi Talv 11mos ago

What would you expect from PHP 9? PHP 8 seems quite mature already. I think they are yet collecting breaking ideas for PHP 9.

Benjamin Crozat
Benjamin Crozat 11mos ago

I'm open to big changes! Some people might not, though. It's really hard to see where it's going for now, unfortunately.

livein
livein 4mos ago

Will PHP 9 support Async/Await mode and will PHP 9's concurrency be perfected and available? May I ask why ruby node these languages, have perfect asynchrony, why PHP so long, already to keep up with the pace of the times, so far there is no perfect asynchrony or coprocessing? Is it because of the development of third-party libraries that some people are deliberately slowing down the progress of PHP asynchrony?

Benjamin Crozat
Benjamin Crozat 4mos ago

I have no idea why PHP is on the trajectory it is now. That being said, it works well and the language's popularity speaks for itself. But maybe you should take a look at ReactPHP if you really need this?

livein
livein 4mos ago

Thank you! Why can't PHP itself suppose these new asynchronous features, but suggests that people go to third party projects. Is the launch of the PHP poll controlled by someone? Is it possible that PHP's organising committee is no longer democratic and has been controlled by a dictatorship for profit?

Manzïny
Manzïny 1mo ago

it’s magnificent and awesome; what makes the power of a language is not its speed or the number of people who train its environment and its use but the stable logic of its use of course many other languages ​​offer facilities and visions of the code exceptional and they must exist to offer solutions to PHP so that PHP can become the best of itself little by little. in its field PHP going too fast is a disadvantage and a danger for many professions. the world of Dev needs stability and not to move forward too quickly. to create solid and powerful foundations.

Get help or share something of value with other readers!

Great deals for enterprise developers
  • Summarize and talk to YouTube videos. Bypass ads, sponsors, chit-chat, and get to the point.
    Try Nobinge →
  • Monitor the health of your apps: downtimes, certificates, broken links, and more.
    20% off the first 3 months using the promo code CROZAT.
    Try Oh Dear for free
  • Keep the customers coming; monitor your Google rankings.
    30% off your first month using the promo code WELCOME30
    Try Wincher for free →
The latest community links
- / -