WordPress and PHP 7.3

PHP 7.3 is in the final stages of its release cycle. As of the publish date of this post, version 7.3.0RC3 has been released and the final release of PHP 7.3.0 is scheduled for December 13th.

As the changes in PHP 7.3 were solidified earlier this year, contributors to WordPress Core worked to identify compatibility issues within the codebase. Overall, the changes needed to declare full PHP 7.3 support are minor, and are all being made.

WordPress aims to fully support PHP 7.3 in its next release.

Here is a breakdown of the changes in PHP 7.3 that plugin and theme developers need to be aware of and should accommodate in their code.

New Function: is_countable()

It is considered good practice to validate variables before using them. Parameters passed to functions may not be of the variable type expected. Similarly, the type of variable returned by a filter may have changed due to incorrect usage of the filter by plugins or themes.

In PHP 7.2, a warning was introduced when count() is used on an uncountable value. PHP 7.3 introduces the is_countable() function to help detect if a variable is in fact countable. This, paired with is_iterable() (introduced in PHP 7.1) allows developers to better follow this best practice.

In WordPress 4.9.6, two polyfill functions were introduced to help developers follow this best practice regardless of the PHP version a site is running. Using these functions in your code will ensure warnings are not triggered when sites are upgraded to newer versions of PHP.

Undefined Variables and compact()

compact() is a function in PHP that creates an array of values from a list of variable names. If an undefined variable is specified, the resulting array will not contain an index for that variable. This could cause some unexpected behavior due to missing indexes and some scenarios that are difficult to debug.

Starting in PHP 7.3, a notice will be triggered when an undefined variable is passed to compact() to help with identify such situations.

switch() and continue

In PHP, applying continue to a switch() statement causes the same behavior as break. However, in other languages, using continue within switch() statements will continue the surrounding loop instead (the equivalent of continue 2).

Starting in PHP 7.3, a warning will be generated when using continue to target a switch() control structure (break should always be used instead). The two instances of this pattern in WordPress Core have been corrected.

while ($foo) {
    switch ($foo) {
        case "bar":
            continue; // This now throws a warning.
            break;    // Use this for equivalent behavior.
            continue 2; // No warning, correctly targets the while loop.
    }
}

Summary

As always, reading through the complete upgrade document is highly recommended.

Even as WordPress Core continues to expand its support for new versions of PHP, there is no intention of abandoning support for older versions until usage numbers show that the impact on users will be minimal. If you wish to help with this, an effort to decrease the usage numbers for older versions of PHP is being lead by the #core-php team through the servehappy initiative.

WordPress continues to encourage all users to run the latest and greatest versions of PHP, including PHP7.3 upon its official release.

A full list of tickets related to PHP 7.3 support can be found on Trac.