New features in PHP 7.3
Prime Numbers
PHP version 7.3 [1] became available at the end of 2018. At first glance, the innovations do not appear spectacular, but they could disrupt existing scripts. That said, with deprecations and end of support for older PHP versions, website owners might want to consider switching to the new version as soon as possible.
PHP 7.3 not only throws some ballast overboard, it also introduces syntax changes. In everyday life, programmers may particularly notice the switch to PCRE2 (Perl-compatible regular expressions, version 2), the changed behavior in multibyte string functions, and the more flexible Heredoc and Nowdoc syntax.
The Here and Now
The Heredoc syntax simplifies the assignment and output of longer text by replacing strings in double quotation marks with text between delimiters. For example, if $name = "Hans Hansen";
, the Heredoc
$output = <<<EOT My name is: "$name". EOT;
would output My name is: Hans Hansen.
In a Heredoc, the <<<
operator is followed by an identifier that signals the beginning of text. The text continues until the identifier appears again to mark the end of the Heredoc.
Nowdoc syntax was introduced in PHP 5.3.0. It behaves like text in single quotes, which PHP does not interpret. If you change the first line of the example to
$output = <<<'EOT'
$output
would display the text My name is: "$name".
In older PHP versions, a semicolon and a blank line always followed the second identifier at the end of the string. The expression EOT; echo $output;
was not allowed, for example. In PHP 7.3, this constraint has been eliminated for both Heredoc and Nowdoc. Moreover, developers can finally indent the text and the identifier:
$output = <<<EOT My name is: Hans. EOT;
The PHP interpreter first determines the number of tabs or spaces before the last identifier and then removes the same number of tabs or spaces padding each line of text above. In the above example $output
contains the text My name is: Hans.
The ability to indent Heredoc and Nowdoc text makes the formatting of classes easier. Heredoc syntax formerly used to enforce this kind of code:
class Person { public $name = <<<EOT Hans Hansen EOT; }
Thanks to PHP 7.3, the code can now take on a more pleasing arrangement:
class person { public $name = <<<EOT Hans Hansen EOT; }
You can easily see that the second alignment is more intelligible.
Commas
In function calls, a comma can now occur after the last parameter, which allows programmers to append additional parameters easily, such as the call:
unset( $alt, $temp, );
PHP programmers can also assign variables by reference in list()
. The three-liner
$array = ["Hans", "Hansen"]; $firstname = $array[0]; $lastname =& $array[1];
can be written succinctly in PHP 7.3 as:
$array = [1, 2]; list($firstname, &$lastname) = $array;
Additions
Two new functions, array_key_first()
and array_key_last()
, determine the first and last keys in an array:
$name = ['Hans' => 1, 'Hansen' => 2]; echo array_key_first($name);
The hrtime()
function returns the current system time in high resolution. Typically, this is the number of nanoseconds that have elapsed after a randomly determined reference point. The is_countable()
function tests whether the variable passed in has countable content; it applies to arrays or objects that implement a Countable
interface.
PHP 7.3 now also supports Argon2id hashes with the introduction of the PASSWORD_ARGON2ID
hashing algorithm constant. The password_hash()
, password_verify()
, password_get_info()
, and password_needs_rehash()
functions now accept Argon2id hashes.
Buy this article as PDF
(incl. VAT)
Buy ADMIN Magazine
Subscribe to our ADMIN Newsletters
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Most Popular
Support Our Work
ADMIN content is made possible with support from readers like you. Please consider contributing when you've found an article to be beneficial.