The Gift of PHP 7.3

DZone 's Guide to

The Gift of PHP 7.3

Read on to unwrap the latest version of PHP, and get a glimpse of all the goodies the PHP dev team has packed into the new update.

· Web Dev Zone ·
Free Resource

Introduction to PHP 7.3 

This Christmas, Rasmus Lerdorf and the PHP development team will give us a new version of the language. 

It will finally be time for PHP 7.3.

From the official release of PHP 7.0 on December 3, 2015,  the releases have been scheduled and deployed with unreal consistency and the list of enhancements have increased exponentially.

Even if there are not major differences between the new version and the one you have been using over the last year, it still worth to going over the new characteristics and how these changes are going to affect your day to day workflow during 2019.

I have divided all the updates into four sections: Syntax, Deprecation, New features, and miscellaneous Changes.

I reckon that using this method you can check out only the features you are interested in, but, my advice is to at least have a quick look at all of them.

  • Syntax
    • Heredoc and Nowdoc syntaxes.
    • Allow a Trailing Comma in Function Calls.
    • list() Reference Assignment.
  • Deprecations
    • Deprecate image2wbmp() function.
    • Deprecate case-insensitive constants.
  • New features
    • is_countable Function
    • array_key_first()array_key_last(). 
    • Same site cookie.
  • Changes
    • PCRE to PCRE2
    • Undefined variables in compact(). 
    • Argon2 Password Hash Enhancements

Implemented Flexible Heredoc and Nowdoc Syntaxes

Heredoc is a polished method for writing strings that allows for the creation of multiple lines of text without the use of the new line characters.

Heredoc evaluates both variables and characters like double quote strings do.

The common use of Heredoc is to create multiple lines of text, SQL queries, or for creating snippets of HTML for emails or the web.

This feature was implemented several versions ago, but it was very strict and forced the developer to pay lots of attention of the syntax and indentation.

The improvements in PHP 7.3 consist of making the implementation of the feature more accommodating and easy to use.

$NorthPolePopulation = ['Santa Claus', 'Msr. Claus', <<<REINDEERS
    REINDEERS, 'Elf Ernest'

An experienced eye might have spotted several changes that are making this snippet of PHP 7.3 much more readable than the ones possible in the previous version.

Let’s start from the indentation. In this new version, the last token (the second REINDEERS in the example above) does not need to be the first string of the new line, as you can see the code is very easy to read and understand. Now, if I need to break the indentation, the line can be indented with both spaces or tabs, it does not matter because they will be trimmed off from the content inside the heredoc. 

A huge difference for me is that, before, the only character permitted after the ending token was the semicolon which meant you had to terminate the command. Now, instead, you can add other expressions. Thus, above, I am showing you how to use the heredoc statement inside other commands, like arrays for instance.

It may seem complicated, but, remember, heredoc and nowdoc are basically just simple strings.

Speaking about retro-compatibility with the older version of PHP, the guys that worked on this update did an amazing job to make it simple. As long as you do not have any strings that contain the same ending token you do not need to do anything; everything is going to work just fine.

$reindeers = <<<REINDEERS

In the example above, the only valid ending token is the last one.

The reason is that the last entry is exactly the same as the opening token whereas REINDEER and CLAUSSREINDEER are going to be displayed as content.

I have barely mentioned this, but just to cover everything, nowdoc is to heredocs what single quoted strings are to double-quoted strings. They are not evaluated for variables or special characters. 

The reason I did not linger on this is that the changes that PHP 7.3 brings are valid for nowdoc as well.

Allow a Trailing Comma in Function Calls

It is scientifically proven that with the same amount of time that a web developer spends fixing syntax error in a script they could write the whole program again from scratch and make no mistakes (okay, it's not, but you get the point).

Some of the most common errors are as simple as forgetting to add a semicolon at the end of a command or add a colon or a character where it is not necessary.

With the new version of PHP one of these problems could be, at least in some cases, solved.

As you have seen in the previous section, it looks like the core language is moving toward a less strict version of itself, which will make the code more customizable while providing more control over error handling and debugging (which we will cover soon).

A trailing comma has been a problem for some web developers. Forgotting to put them in somewhere or adding them where PHP does not allow them can quickly turn into a complete nightmare.

 In previous versions of PHP, lines of code like the one below would result in the following error message:

santaBag('football', 'dolly', 'family portrait', 'Monopoly',);
// PHP Parse error:  syntax error, unexpected ')'

Now, instead, as long as you use only one comma, you can add it at the end of the list and the script will parse smoothly.

The same concept works for arrays.

Using the same example I have used before:

$reindeers = [“Rudolph”,

Leaving the last comma over there won’t throw any error.

I am a firm supporter of working without stress and coding with peace of mind (maybe even listening to some ambient noise or soft-jazz on the background?).

Having to interrupt the flow of coding in order to go back and fix these little syntax issues, maybe even just only two or three times in the whole day will make your stress quickly escalate, which eventually will make your code look sloppy.

Not having to worry about these little syntax mistakes will substantially improve the overall quality of the deployed project.

Something to bear in mind is that this new rule does not effect functions or method declarations.

function delivery($day, $month, ) 

Notice that the snippet above will still return an error. To avoid this, you can use variadic functions via the (dot dot dot) keyword released in PHP5.6.

Regarding backward compatibility, there is no problem at all.

The code you have already written is going to continue to work fine.

The only parts I suggest refactoring, if you need to clean up your code a bit, is swapping the variadic parameters just seen to this trailing commas.

But do not add the trailing comma on every array and method you see, use this new feature sparingly.

List() Reference Assignment

List is a PHP construct (a command like echo) that can be immensely powerful if you know how to use it.

It is used to allocate multiple variables in one operation, the syntax is a little bit is weird but once understood it can be easily implemented.

Here is an easy example:

$christmasWords = ["Santa", "distributes" ,"gifts"];
list($subject, $verb, $object) = $christmasWords;
echo "$subject $verb the $object on christmas day";

In here, you can see an array containing Christmas words. What we want to do is to divide it and give a singular variable to each of the elements in the array.

This differs from explode() as the process is less automatic but gives to the programmer more control.

Here is another example:

list($subject,, $object) = $christmasWords;

In this case “distributes” (the second element of the array) will not be assigned.

Here's a more difficult example that will let you see how powerful this command is:

$Passport = ['locality' => 'North Pole', 'position' => 'Santa Claus'];
list('position' => $role, 'locality' => $location) = $Passport;
Echo $role; // will output Santa Claus
Echo $location; // will output North Pole

Now, talking about the changes to the 7.3 version of PHP, the RFC, or reference assignment, on the list() method was requested for the first time about 18 years ago as a bug on the reference assignment.

Consider the following command:

$SantaClaus = &$SaintNicholas;

In the previous case, the variable created is not copying the value of the existing variable but, the two variables point to the same data.

This means that when there is a change in any variable it effects the original data.

$SaintNicholas = "Saint Nicholas";
$SantaClaus = &$SaintNicholas;
Echo $SaintNicholas; // echoes "Saint Nicholas"
Echo $SantaClaus; // echoes "Saint Nicholas"
$SaintNicholas = "Father Christmas";
Echo $SaintNicholas; // echoes "Father Christmas"
Echo $SantaClaus; // echoes "Father Christmas"

How does list() change with PHP 7.3?

PHP 7.3 allows us to assign variables by references. The advantage of this is that now we can edit a single variable rather than find the index within the array.

$reindeers = ["Rudolph","Blitzen"];

list($zero, &$first) = $reindeers;
$first = 'Vixen';
echo $reindeers[1]; //echoes "Vixen"

Even though this was a long-requested feature of the language, the PHP community now prefers to do it via value objects, which will be passed by reference in any case and make the code easier to understand.


If you have already read some of my previous blog posts, you know that I love to go into details and dig deep in order to provide the most value possible. 

In this first part of the article, we saw some of the syntax difference between PHP 7.2 and the new upcoming version.

These are just a few of a more complete list of updates that the team of developers lead by Rasmus Lerdorf have implemented.

After a very long stop, during the past years, the community of PHP developers all around the world has enjoyed a new version of the language on a yearly basis.

The improvement is in the public eye, and figures to show that PHP 7.x has nothing to envy to more trendy server-side languages such as Python and Node.js.

While waiting for the breaking change of version 8, stay tuned and subscribe to my newsletter to get notified when the other parts of this article will be published.

To Be Continued...

This blog post is a section of the Kindle book, PHP 7.3 and its Christmas gifts.

Now It's Your Turn

I hope you enjoyed this syntax review. Now I want to hear from you: What new feature of PHP 7.3 are you going to use more ofter?

Let me know by leaving a quick comment below.

php 7.3 ,php tutorial ,php tutorial for beginners ,web dev

Published at DZone with permission of Nico Anastasio . See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}