Dev & EngARTICLE

PHP 7.3: check out what's new in this version

The latest PHP 7.3 release is arriving in full force with features to help developers. Check out this version's new functionality and improvements.

PHP 7.3: check out what's new in this version
Image: Dionatha Rodrigues

As you probably know, PHP is gaining strength again, and the community is working full steam to improve the language's syntax as well as its performance.

The latest PHP 7.3 release is arriving in full force with features to help developers. In this version, we'll have new functionality, performance improvements, some deprecations, and a good number of bug fixes.

In this article, we've gathered the main changes so you can stay up to date before updating your server. Read until the end to learn about the news.

Reference assignments in list()

PHP has had list() assignment and assignment by reference for a long time. However, until now it wasn't possible to use reference assignment with list(). The new version brings a syntax that allows this.

It's interesting to note that requests for this implementation date back almost 20 years, as we can see in this and this request.

The new syntax looks like this:

php
$array = [1, 2];
list($a, &$b) = $array;[/php]
Which is equivalent to:
[php]$array = [1, 2];
$a = $array[0];
$b =& $array[1]

This new approach can also be used in nested lists, as you can see in the complete specification in PHP's official documentation.

Throwing exceptions for errors in json_encode and json_decode

Currently, PHP has two functions for handling JSON: json_decode() and json_encode(), but neither of them has ideal error handling.

json_decode() returns NULL on error, but NULL is also a valid result when decoding the JSON "null". The only way to know if an error occurred is by calling json_last_error() or json_last_error_msg(), which returns the global error state.

However, simply changing the default behavior of these functions to throw an exception could be a major backward-compatibility problem.

In version 7.3, a new flag (JSON_THROW_ON_ERROR) will be added as an option for json_decode(). When this flag is passed, the error behavior of these functions changes. The global error state isn't modified, and a JsonException exception is thrown.

The is_countable() function

In PHP 7.2, a Warning was added when trying to count uncountable objects, and because of that, all developers were forced to find and change their code to avoid this Warning. Code similar to the following started being used to avoid this Warning:

php
if (is_array($foo) || $foo instanceof Countable) {
return count($foo);
}[/php]
So a method was implemented that returns TRUE or FALSE to indicate whether an object is countable or not. The syntax looks like this:
[php]if (is_countable($foo)) {
// $foo is countable
}

More examples can be found in PHP's official Wiki.

Flexible syntax for Heredoc and Nowdoc

The rigid syntax of heredoc and nowdoc causes developers to avoid them, since their use can look ugly and hurt readability. Because of this, two changes were implemented to the current heredoc and nowdoc syntax to improve these issues:

  • Allowing the closing marker to be indented;
  • Removing the new line requirement after the closing marker

For example:

Previously, the code had to be:

php
<?php
class foo {
public $bar = <<<EOT
bar
EOT;
}

And now it can be (note the indentation):

php
<?php
class foo {
   public $bar = <<<EOT
   bar
   EOT;
}

Other changes were made to the syntax, such as the need to break the line after the closing marker. All the changes made to this feature can be checked in detail in the PHP RFC.

The new PHP version also brings other features, bug fixes, and the deprecation of some methods. You can find the list of methods that will be removed in future versions in this link.

Trailing comma in function and method calls

Allowing a "trailing comma" in function calls makes it more convenient to add arguments in contexts where it's common to call functions with many arguments. This is very useful for VCS (GIT, TFS, SVN, CVS, etc.), since individual lines in the list can be modified, added, or removed without needing to touch unrelated lines at the end of the list.

This means that inserting additional parameters into calls will avoid touching the previous line to add a new comma, which is bad for version control history. Some examples of using this new syntax:

Initializing an array

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

Calling a method

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

Passing parameters to a template engine

php
echo $twig->render(
'index.html',
compact(
'title',
'body',
'comments',
)
);

More usage examples can be found in PHP's official Wiki.

Availability

Most servers are already preparing to receive the new version on their cPanel servers, and we at DialHost aren't falling behind. Starting in April 2019, all customers will be able to update their PHP version within the Control Panel, and if you're not a customer yet, check out our services.

Translated from the Brazilian Portuguese original · Read the original