Wed Sep 05 2018

What’s new in PHP 7.3

New features in PHP 7.3

PHP is the world's most popular server-side programming language. It powers large parts of the web with products such as WordPress, MediaWiki and so on. Most recently, the PHP team had announced and released the first PHP 7.3.0 version - Alpha 1. Now it comes with new useful features, functionalities, deprecations, and a good number of bug fixes, though which is based on PHP 7.2. This release is all about web developers.

In this post, we explore an overview of the features and changes of PHP 7.3-

Heredoc and Nowdoc Syntaxes

This is one of the most relevant improvements coming with PHP 7.3, and we think it deserves a little more attention. The heredoc syntax provides a way of adding a large amount of text without the need to escape things like double quotes. A heredoc starts with <<< followed by a marker, and ends with the same marker followed by a semicolon. A nowdoc behaves much like a heredoc, with some exceptions - the identifier is enclosed in single quotes (<<<'EOT') and no parsing is done inside a nowdoc.

Closing marker

Remember, the closing marker must begin in the first column of the line and the marker must follow the same naming rules as any other label in PHP; it must contain only alphanumeric characters and underscores, and must start with a non-digit character or underscore. Allow for the closing marker to be indented and for the leading whitespace to be stripped. The indentation of the closing marker sets the amount of whitespace (or tabs) that will be stripped from each line of the body. But be careful: the closing marker should never be indented further than any other line of the body.

Stripping tabs and whitespaces

Stripping tabs and whitespaces allow us to indent the body of the heredoc/nowdoc to the same level of the code around, and without unnecessary whitespace before each line of the body. You can use both tabs and spaces for indentation, but you are not allowed to use them intermixed. This means that you must use the same indentation characters for the closing marker and any lines of the body. In case of different indentation characters, expect a different type of parse error (invalid indentation).

Remove the Trailing New Line

Currently, a new line must follow the marker in order to terminate the heredoc/nowdoc. PHP 7.3 would change this and would allow us to terminate the heredoc/nowdoc on the same line.

A trailing comma in function calls

As of PHP 7.3, trailing commas would be allowed in function declarations. Trailing commas (or “final commas”) are commas appended to a list of elements, parameters or properties and they come in handy in contexts where new values are appended frequently because they prevent errors due to a missing comma. In the previous PHP, trailing commas are allowed in arrays. You can use a trailing comma when we are creating an array with compact(), in order to return a formatted string with sprintf(), or when merging an array. Also, trailing commas would be useful for debugging. Trailing commas will be allowed in method calls and enclosures, as well.

JSON errors

PHP 7.3 provides a new way of handling JSON errors. This is not a core feature but in addition to the JSON extension that would change the error behavior of json_decode() and json_encode(). json_encode() returns FALSE on error. This is clearer because there is a specific error value. Both functions neither halt program execution on error nor throw any warning.

The list() Reference Assignment

In PHP we can assign a value by reference, meaning that two variables may point to the same data, and every change to any variable affects the original data.

The list() Construct

The list() language construct can be used to “assign variables as if they were in an array”, but with list() you are not currently allowed to assign variable values by reference. PHP 7.3 should change this through allowing you to assign variables by reference also with the list() construct. The advantage of this proposal is that we could now assign multiple variables by reference.

The is_ countable Function

Another useful feature coming with PHP 7.3 is the is_ countable() function. This RFC proposes the function is_countable(), which returns true if the given variable is an array or it is a countable variable, false otherwise.

The array_key_first() and array_key_last()

Currently, we can retrieve the first and the last key of an array by using reset(), end() and key() functions. Unfortunately, with these functions, there’s no way to gather the first or the last index of an array without changing its internal state. This proposal would change this scenario by adding two new functions to the PHP core. In PHP 7.3, array_key_first() and array_key_last() allow retrieving the first and the last key of a given array without affecting the internal array pointer. These new functions would allow writing less complex code and in some cases avoid errors.

Deprecations

The following functions/functionalities will be deprecated with PHP 7.3 -

  • image2wbmp() function from GD extension is used to output the WBMP (Wireless Bitmap) format of an image. This function is deprecated in PHP 7.3 in favor of imagewbmp function. If you are using image2wbmp function, simply replace the function name with imagewbmp and you are good to go! It looks like the PHP team deprecated the less used function over to minimize the impact.

  • If you are using image2wbmp function, replace the calls with imagewbmp. Look for automated tools that can fix this for you as well.

  • When you use filter_var($var, FILTER_VALIDATE_URL), there are two additional flags you could use to ensure the URL is strictly validated - FILTER_FLAG_SCHEME_REQUIRED and FILTER_FLAG_HOST_REQUIRED.

  • fgetss() function and string.strip_tags filter

  • String search functions with integer needle.

  • pdo_odbc.db2_instance_name php.ini directive

 

You can download the current PHP 7.3 version for your development and testing, but keep in mind that, this should not currently be used in production environments.

We use cookies to improve your experience on our site and to show you personalised advertising. Please read our cookie policy and privacy policy.