PHP 7 Features Every WordPress Developer Should Know

PHP 7.0 was released in December 2015 and brought significant performance gains — roughly twice as fast as PHP 5.6 — along with new language features that make code cleaner and more expressive. WordPress 4.x requires PHP 5.6+, so all of these features are safe to use in themes and plugins targeting modern hosting environments.

Problem: Which PHP 7 language features provide the most practical value for WordPress plugin and theme development?

Solution: The most useful additions are scalar type declarations, return types, the null coalescing operator (??), the spaceship operator (<=>), and anonymous classes — all of which reduce boilerplate and make intent clearer.

// Return type declarations (PHP 7.0)
function get_post_count(): int {
    return (int) wp_count_posts()->publish;
}

// Nullable types — accepts the type or null (PHP 7.1)
function find_user( int $id ): ?WP_User {
    $user = get_user_by( 'id', $id );
    return $user ?: null;
}

// Null coalescing operator ?? (PHP 7.0)
// Returns left side if it exists and is not null, otherwise right side
$page = $_GET['page'] ?? 'home';
$slug = $post->post_name ?? 'unknown';

// Spaceship operator <=> for sorting (PHP 7.0)
usort( $posts, function( $a, $b ) {
    return $a->post_date <=> $b->post_date;
} );

// Scalar type declarations (PHP 7.0)
function format_price( float $price, string $currency = 'USD' ): string {
    return number_format( $price, 2 ) . ' ' . $currency;
}

// list() shorthand with [] (PHP 7.1)
[ $first, $second ] = explode( ',', 'foo,bar' );

// Constants in class arrays (PHP 7.0)
class My_Plugin {
    const SUPPORTED_TYPES = [ 'post', 'page', 'portfolio' ];
}

PHP 7.2 (December 2017) added object as a parameter and return type, and the Sodium cryptography extension to core:

// object type hint (PHP 7.2)
function process( object $data ): object {
    $data->processed = true;
    return $data;
}

NOTE: Adding strict type declarations (declare(strict_types=1); at the top of a file) makes PHP throw a TypeError instead of silently coercing values. This is excellent practice in new code but can break existing code that relies on implicit type juggling.