Lead Image © MIR, Fotolia.com

Lead Image © MIR, Fotolia.com

New features in PHP 7.3

Prime Numbers

Article from ADMIN 51/2019
By
The new PHP 7.3 simplifies string handling, supports PCRE version 2, adds LDAP controls, improves logging, and deprecates some features, functions, and syntax elements.

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

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy ADMIN Magazine

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

  • Lua for Apache

    Lua is a small, lean, and fast scripting language – ideal for working with web servers. Version 2.4 of the Apache web server is the first to offer a matching module that has a few quirks – and pitfalls, if you dig more deeply.

  • Program from one source to many apps with Flutter
    Developing apps in the past for Android, iOS, the web browser, and the desktop meant having to write different versions of the code; however, that's no longer the case thanks to Google's Flutter framework.
  • Innovations in PowerShell 5
    Windows 10 brings an updated, fifth release of PowerShell that vastly simplifies the task of managing modules and software packages. The scripting language now also handles various formatted output from commands and selection lists.
  • The Lua scripting language
    The Lua scripting language is built in to a variety of programs. In this article, we provide an insight into the Lua cosmos.
  • The Lua Scripting Language

    Is this powerful but simple scripting language big enough for Big Data?

comments powered by Disqus
Subscribe to our ADMIN Newsletters
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs



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.

Learn More”>
	</a>

<hr>		    
			</div>
		    		</div>

		<div class=