Top fetaures of PHP 7.3

Amit Merchant · December 8, 2018

PHP development team has just released PHP 7.3 with general availability. This release brings general improvements along with some new features. Even though this is a stable release, the team hasn’t provided concrete migration guide if you want to migrate from the older PHP versions. Maybe they will release those on a later date.

So, meanwhile let’s look at all the new features which are available in PHP 7.3.

More Flexible Heredoc and Nowdoc Syntax

The closing marker for doc strings is no longer required to be followed by a semicolon or newline. Additionally the closing marker may be indented, in which case the indentation will be stripped from all lines in the doc string. This means that the following syntax will be valid.

class foo {
    public $bar = <<<EOT
bar
    EOT
}
// Identifier can be indented now
// Also notice there is no semicolon after closing marker

Trailing Commas are allowed in Calls

Trailing commas in function and method calls are now allowed in PHP 7.3. A trailing comma has been allowed in array syntax since forever-ever, and in grouped namespace syntax since PHP 7.2. Below for example,

$foo = [
    'foo',
    'bar',
];

The similar syntax can now be utilized for function calls as well. Take for the example,

unset(
    $foo,
    $bar,
    $baz,
);

Array Destructuring supports Reference Assignments

Array destructuring now supports reference assignments using the syntax [&$a, [$b, &$c]] = $d. The same is also supported for list().

CompileError Exception instead of some Compilation Errors

A new CompileError exception has been added, from which ParseError inherits. A small number of compilation errors will now throw a CompileError instead of generating a fatal error. Currently, this only affects compilation errors that may be thrown by token_get_all() in TOKEN_PARSE mode, but more errors may be converted in the future.

An is_countable() function

PHP 7.3 comes with a is_countable() which can be used to verify if contents of a variable is an array or an object implementing Countable.

var_dump(is_countable([1, 2, 3])); // bool(true)
var_dump(is_countable(new ArrayIterator(['foo', 'bar', 'baz']))); // bool(true)
var_dump(is_countable(new ArrayIterator())); // bool(true)
var_dump(is_countable(new stdClass())); // bool(false) 

This were the new features which I felt are the key ones of PHP 7.3. If you wish to check out the PHP 7.3, you can download it from here or check the changelog from the official site.

Newsletter