Introduction to PHP 7.3
It’s the night of the 24th of December,
Despite the period the house is warm and smell “sentimental” mostly because of the woods that have been burning in the fireplace the previous days.
The blinking lights of the Christmas tree are shining over a 9-courses dinner table that I have just finished to eat with the whole family.
I am now putting on my favorite flannel pyjamas of the power ranger (remember those?).
I set up the alarm of my clock at 7:00 even though I already know that it will be useless since I will be awake much before the alarm will go off.
I can’t wait to wake up!
just like the same day over the past 4 or 5 years.
In the morning, few minutes after the dawn has started, I jump off the bed and run downstairs literally diving under the tree.
As you may have understood this episode happen several years ago but the euphoric feelings of giving and receiving new stuff that later I have learned is simply caused by the release of endorphins into their brain still persists nowadays without any power from my conscious side.
After almost 20 years, have moved in 2 countries and dozens of job experiences in my background I still love Christmas.
And the same, I am sure, is for Rasmus Lerdorf and his team.
This Christmas in fact, the development team of PHP 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, the third of December 2015,
the releases have been scheduled and deployed with unreal consistency and the list of enhancement has increased exponentially.
Even if there will not be major differences between the new version and the one you have been using during the last year, it still worth to go over the new characteristics and check how these are going to affect your day to day workflow during 2019.
I have divided all the updates into four sections, that are: 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.
Heredoc And Nowdoc syntaxes
Allow a Trailing Comma in Function Calls
list() Reference Assignment
Deprecate image2wbmp() function
Deprecate FILTER_FLAG_SCHEME_REQUIRED and FILTER_FLAG_HOST_REQUIRED flags used with FILTER_VALIDATE_URL
Deprecate case-insensitive constants
Same site cookie
PCRE to PCRE2
Undefined variables in compact()
Argon2 Password Hash Enhancements
Implemented Flexible Heredoc And Nowdoc Syntaxes
Heredoc is a polished method of writing strings that allows the creation of multiple lines of text without the use of the new line characters.
Heredoc evaluates both variables and characters like double quotes 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 has been implemented several versions ago but it was very strict and forced the web developer to pay lots of attention of the syntax and indentation.
The improvements in PHP 7.3 consist in 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 previous example) 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 that it would be if I need to break the indentation,
the line can be intended 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,
you can add other expressions, thus, as I am showing above you can use the heredoc statement inside other commands like for instance arrays.
It could seem complicated to use but remember eventually heredoc and nowdoc are 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 string that contains 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 is exactly the same as the opening token whereas REINDEER and CLAUSSREINDEER are going to be displayed as content.
A little parenthesis.
I have barely mentioned but just to cover everything nowdoc is to heredocs what single quoted 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 proved that with the same amount of time that a web developer spends fixing syntax error in a script he could write the whole program again from scratch and no mistake.
(Ok is not, but you got 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 is not necessary.
With the new version of PHP one of this problem could be, at least in some case, solved.
As you have seen in the previous section looks like the core language is moving toward a less strict version of itself which will make the code more customizable meanwhile providing more control on error handling and debugging (you will see it soon).
A trailing comma has been a problem for some web developers, forgot to put them somewhere needed or adding them where PHP does not allow can turn quickly in a complete nightmare.
On version before PHP7.3 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 while you are is a state of peace of mind, maybe even listening at 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 2 or 3 times in the whole day making the stress quickly escalate, which eventually will make the code look sloppy.
Do not have to worry about this little syntax mistakes will improve substantially the overall quality of the project deployed.
Something to bear in mind is that this new rule does not affect functions or methods declarations.
function delivery($day, $month, )
Notice that the snippet above will still return in an error
To avoid it 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 to refactor if you need to clean up 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 with thriftiness.
List() Reference Assignment
List is a PHP construct (a command like echo to make it simple) 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 buy once understood how it works 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 in this case is to divide it and give a singular variable to each of the element of the array.
This differs from explode() inasmuch 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.
A more difficult example that will let you see how powerful is this command:
$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
Talking about the changing on the 7.3 version of PHP, the RFC of reference assignment on list() has been requested for the first time about 18 years ago bug on reference assignment.
Considering 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.
It means that when there is a change in any variable it affect 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 it is that now we could edit a single variable rather than find the index within the array.
$reindeers = ["Rudolph","Blitzen"];
list($zero, &$first) = $reindeers;
$first = 'Vixen';
echo $reindeers; //echoes "Vixen"
Since this is a brand new feature and it was not possible to use this feature before there is no backward compatibility problem.
However, 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,
dig deep in order to provide the most complete value possible.
In this first part of the article, you have seen some of the syntax difference between PHP 7.2 and the new upcoming version.
These above are just a few of a more complete list of updates that the team of developer lead by Rasmus Lerdorf has 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 show the PHP 7.x has nothing to envy to more trendy server-side languages such as Python and NodeJs
While waiting for breaking change of the 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.