PHP 7 features worth using in WordPress code
PHP 7.0 came out in December 2015 and was roughly twice as fast as PHP 5.6 on WordPress, but it also brought language features that make code shorter and clearer. WordPress itself still runs on PHP 5.2.4 and newer, so core cannot use them, but in your own themes and plugins for servers you control they are safe to use.
Problem: Theme and plugin code is written as if it were still PHP 5, with long isset() checks and no types.
Solution: On PHP 7.0+ use the null coalescing operator, scalar and return types, and the spaceship operator; on 7.1+ also nullable types and the short [] list syntax. Declare the required PHP version in the plugin header and check it on activation.
The null coalescing operator
?? returns the left side if it exists and is not null, otherwise the right side, without a notice for a missing key:
<?php // PHP 5 $page = isset( $_GET['tab'] ) ? sanitize_key( $_GET['tab'] ) : 'general'; // PHP 7.0 $page = sanitize_key( $_GET['tab'] ?? 'general' );
Scalar types and return types
<?php
// PHP 7.0: int, float, string and bool in signatures, and a return type
function ha_format_price( float $price, string $currency = 'USD' ): string {
return number_format( $price, 2 ) . ' ' . $currency;
}
// PHP 7.1: nullable types, the value can be the type or null
function ha_find_user( int $id ): ?WP_User {
$user = get_user_by( 'id', $id );
return $user ?: null;
}
Be careful with types in callbacks of WordPress hooks: filters often pass values of an unexpected type (a string instead of an int, null instead of an array). A strict type in a callback turns such a value into a fatal TypeError on the site.
The spaceship operator
<=> returns -1, 0 or 1, which is exactly what usort() wants:
<?php
usort( $posts, function ( $a, $b ) {
return strcmp( $a->post_date, $b->post_date ); // PHP 5
} );
usort( $posts, function ( $a, $b ) {
return $a->post_date <=> $b->post_date; // PHP 7.0
} );
Smaller things
<?php
// PHP 7.0: arrays in define()
define( 'HA_POST_TYPES', array( 'post', 'page', 'portfolio' ) );
// PHP 7.1: short list syntax
[ $width, $height ] = explode( 'x', '1200x800' );
// PHP 7.2: the object type
function ha_mark_processed( object $data ): object {
$data->processed = true;
return $data;
}
PHP 7.2 also added the Sodium extension for modern cryptography to core.
Requiring PHP 7 in a plugin
If an older PHP loads a file with this syntax, the site shows a parse error, before any of your code can run. Keep the main plugin file PHP 5 compatible and load the PHP 7 code only after a check:
<?php
/**
* Plugin Name: My Plugin
*/
if ( version_compare( PHP_VERSION, '7.0', '<' ) ) {
add_action( 'admin_notices', function () {
echo '<div class="notice notice-error"><p>My Plugin requires PHP 7.0 or newer.</p></div>';
} );
return;
}
require __DIR__ . '/includes/plugin.php';
For plugins in the official directory, also add Requires PHP: 7.0 to readme.txt.
NOTE: declare( strict_types=1 ); at the top of a file makes PHP throw a TypeError instead of converting values silently. It applies to calls made from that file, so it is good for your own classes, but test hook callbacks carefully, for the reason above.