version_compare()
version_compare() — Compares two "PHP-standardized" version number strings.
Built-in function of PHP 8.5. The manual lists it as available in PHP 4 >= 4.1.0, PHP 5, PHP 7, PHP 8. This page is the reference card: signature, parameters and return value, plus the changelog and the example the manual provides.
Signature
version_compare(string $version1, string $version2, ?string $operator = null): int|bool
Parameters
| Parameter | Type | What it takes |
|---|---|---|
| $version1 | string | First version number. |
| $version2 | string | Second version number. |
| $operator | ?string | An optional operator. The possible operators are: <, lt, <=, le, >, gt, >=, ge, ==, =, eq, !=, <>, ne respectively. |
Return value
By default, version_compare() returns -1 if the first version is lower than the second, 0 if they are equal, and 1 if the second is lower. When using the optional operator argument, the function will return true if the relationship is the one specified by the operator, false otherwise.
Example
A runnable snippet:
if (version_compare(PHP_VERSION, '7.0.0') >= 0) { echo 'I am at least PHP version 7.0.0, my version: ' . PHP_VERSION . "\n"; } if (version_compare(PHP_VERSION, '5.3.0') >= 0) { echo 'I am at least PHP version 5.3.0, my version: ' . PHP_VERSION . "\n"; } if (version_compare(PHP_VERSION, '5.0.0', '>=')) { echo 'I am at least PHP version 5.0.0, my version: ' . PHP_VERSION . "\n"; } if (version_compare(PHP_VERSION, '5.0.0', '<')) { echo 'I am still PHP 4, my version: ' . PHP_VERSION . "\n"; }
See also
phpversion(), php_uname(), function_exists()
All PHP 8.5 built-in functions
Reference card built from the PHP manual page version_compare() (PHP documentation, CC BY 3.0). The page is generated from the manual, so the wording of the parameter and return value text follows php.net.