is_php_version_compatible()WP 5.2.0

Checks compatibility with the current PHP version.

1 time — 0.000001 sec (speed of light) | 50000 times — 0.02 sec (speed of light) | PHP 7.2.16, WP 5.2

No Hooks.

Return

true|false. True if required version is compatible or empty, false if not.

Usage

is_php_version_compatible( $required );
$required(string) (required)
Minimum required PHP version.

Examples

0

#1 Examples of test results

phpversion(); // 7.2.10

is_php_version_compatible( '5' );           // true
is_php_version_compatible( '5.3.0-dev' );   // true
is_php_version_compatible( '5.6' );         // true
is_php_version_compatible( '5.6.11' );      // true
is_php_version_compatible( '7' );           // true
is_php_version_compatible( '7.2' );         // true
is_php_version_compatible( '7.2.09' );      // true

is_php_version_compatible( '7.2.10' );      // true

is_php_version_compatible( '7.2.11' );      // false
is_php_version_compatible( '7.2.12' );      // false
is_php_version_compatible( '7.3' );         // false
is_php_version_compatible( '8' );           // false
0

#2 Output a message about an outdated version of PHP

Suppose we have written a plugin that will run only on PHP v7 and above. Then we must provide that the main code of the plugin will not run if the version on the user's server is lower than required.

Suppose we use PHP 5.6.11 installed on the server, then:

if ( is_php_version_compatible( '7.0.0' ) ) {
	require_once __DIR__ . '/main-file-plugin.php';
}
else {
	add_action( 'admin_notices', 'admin_php_version__error' );
}

function admin_php_version__error() {
	?>
	<div class="notice notice-error">
		<p>You are using an old version of PHP. Ask your server administrator to update it!
	</div>
	<?php
}

The code above is incomplete and only shows how the notification should work.

Changelog

Since 5.2.0 Introduced.

is_php_version_compatible() code WP 6.5.3

function is_php_version_compatible( $required ) {
	return empty( $required ) || version_compare( PHP_VERSION, $required, '>=' );
}