PHP, Javascript & React

Handy Features of PHP 7.x

By:

Mark Biek

on 9/20/2019

Last Fall, in preparation for PHP 5.6 reaching end-of-life at the beginning of 2019, we went through the process of updating all of our PHP sites for PHP 7.0.

Then, more recently, we went through a smaller migration to take everything from PHP 7.0 to 7.1 and finally 7.2 (and now we’re making plans for 7.3, phew!).

New and interesting PHP features

Here are some interesting PHP features and tidbits we’ve discovered and/or been able to start using after all of this upgrading.

For all the details, check out the official announcements for each release.

Null Coalescing Operator

Available as of PHP 7

Where you used to write $value = isset($array['val']) ? $array['val'] : 'default';

now you can write $value = $array['val'] ?? 'default';!

Type Declarations

Sort of available as of PHP 5, better in PHP 7.

PHP 5 allowed simple function argument type declarations (formerly “type hints”) for a small number of types. PHP 7 updates things to account for all the major types and also throws a TypeError exception on failure.

For example: function myFunc (bool $param) {}. If $param isn’t a bool, a TypeError will be thrown.

PHP 7 also adds return type declarations like function myFunc (bool $param): string {}. In this case, a TypeError will be thrown if myFunc doesn’t return a string.

Checkout the PHP docs for more details.

Variable-length argument lists

Available as of PHP 5 (but I only just learned about it)

Add ... to a function argument list to denote the function accepts a variable number of arguments.

function myFunc (...$things) { }

In this case, $things will be an array of all the arguments passed to the function.

Group use declarations

Available as of PHP 7

catch can specify multiple exceptions

Available as of PHP 7.1

Pre-PHP 7.1, if you wanted to handle multiple exceptions with the same code, you had to do something like

Now, we can do

The path to PHP 7.x

There are a number of helpful tools for checking PHP compatibility. Our favorites are

Both of these tools can be used to scan a PHP site and point out places that need to be updated. For our WordPress sites, most errors we got were from plugins that were easily updated. For non-WordPress PHP sites, we use composer for most libraries which also made updating easy.

We have a handful of sites still running Magento 1.9.x. In those cases, we installed the Inchoo PHP 7 Magento extension which provided PHP 7 compatibility for those sites. You can also get official PHP 7.2 support patches directly from Magento.

While the compatibility scanners are handy, they didn’t catch everything. Luckily, we have New Relic error analytics setup. This handy service sends emails when a site is throwing excessive PHP errors and/or warnings.

We used the New Relic analytics reports to find and fix any lingering PHP 7 compatibility issues.

Share to

Related Posts

Wordpress to Sanity Data Script

By:Nick Stewart on 3/11/2021

One of the biggest challenges of moving from one CMS to another is getting your existing data to the new platform. In our case, we were moving our existing WordPress platform, which had been around for years, over to the Sanity.io platform.

Read More »
Developing the New via.studio

By:Alec Robertson on 6/14/2021

A deep dive into creating a fast, user-focused experience on the latest web technology stack for the new via.studio website.

Read More »