is_php_version_compatible()
Compares the provided PHP version with the PHP version installed on the server.
Works based on the function version_compare() with the condition >=.
See also the analogous WP version comparison: is_wp_version_compatible().
No Hooks.
Returns
true|false. True - the specified PHP version is compatible or the version for compatibility check is not specified ('', false, etc.). false - the specified version is not compatible.
Usage
is_php_version_compatible( $required );
- $required(string) (required)
- The minimum required PHP version.
Examples
#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
#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() is php version compatible code WP 6.9.1
function is_php_version_compatible( $required ) {
return empty( $required ) || version_compare( PHP_VERSION, $required, '>=' );
}