What’s New in PHP with PHP 7.4?

Posted on

In this post we’re covering several changes and features that should be added to the language with the final release of PHP 7.4:

PHP 7.4 is the upcoming minor release that will make PHP faster and more reliable. ????Check out our deep dive into the new features!

Forget array_merge: PHP 7.4 Brings Spread Operator in Array Expression
Available since PHP 5.6, argument unpacking is a syntax for unpacking arrays and Traversables into argument lists. To unpack an array or a Traversable, it has to be prepended by … (3 dots), as shown in the following example:

function test(…$args) { var_dump($args); }
test(1, 2, 3);

Now this PHP 7.4 RFC proposes to extend this feature to array definitions:

$arr = […$args];

The first declared benefit of Spread Operator in array expression is all about performance. In fact, the RFC doc states:

Spread operator should have better performance than array_merge. That’s not only because the spread operator is a language structure while array_merge is a function, but also because compile time optimization can be performant for constant arrays.

A significant advantage of a Spread operator is that it supports any traversable objects, while the array_merge function only supports arrays.

Here is an example of argument unpacking in array expression:

$parts = [‘apple’, ‘pear’];
$fruits = [‘banana’, ‘orange’, …$parts, ‘watermelon’];

If you run this code within PHP 7.3 or earlier, PHP throws a Parse error:

Parse error: syntax error, unexpected ‘…’ (T_ELLIPSIS), expecting ‘]’ in /app/spread-operator.php on line 3

Instead, PHP 7.4 would return an array:

array(5) {
string(6) “banana”
string(6) “orange”
string(5) “apple”
string(4) “pear”
string(10) “watermelon”

PHP 7.4 has been working on preloading options, short closures, custom object serialization, FFI work that did not end up making it for PHP 7.3, the null coalescing assignment operator has been added, and various other changes. The PHP 7.4 alpha releases are supposed to kick off on 6 June while the betas will then fire up beginning on 18 July followed by at least six release candidates beginning at the end of August. If all goes well, PHP 7.4.0 will make its debut around the end of November or beginning of December. PHP-7.4 has been branched since January while PHP-8.0 development is on Git master for that next major PHP8 release with JIT functionality and other changes in the works.

Curious about the pre-alpha performance state of PHP 7.4, I ran some quick benchmarks today against PHP 7.3.6, 7.2.18, 7.1.29, and 7.0.32 built from Git and each release built in the same manner.

What’s New in PHP with PHP 7.4
PHP 7.4 Benchmark