|
|
 |
version_compare (PHP 4 >= 4.1.0, PHP 5) version_compare --
Compares two "PHP-standardized" version number strings
Descriptionint version_compare ( string version1, string version2 [, string operator] )
version_compare() compares two
"PHP-standardized" version number strings. This is useful if you
would like to write programs working only on some versions of
PHP.
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.
The function first replaces _, -
and + with a dot . in the version
strings and also inserts dots . before and after any
non number so that for example '4.3.2RC1' becomes '4.3.2.RC.1'. Then it
splits the results like if you were using explode('.', $ver). Then it
compares the parts starting from left to right. If a part contains
special version strings these are handled in the following order:
dev < alpha =
a < beta =
b < RC <
pl. This way not only versions with different levels
like '4.1' and '4.1.2' can be compared but also any PHP specific version
containing development state.
If you specify the third optional operator
argument, you can test for a particular relationship. The
possible operators are: <,
lt, <=,
le, >,
gt, >=,
ge, ==,
=, eq,
!=, <>,
ne respectively. Using this argument, the
function will return 1 if the relationship is the one specified
by the operator, 0 otherwise.
Note:
PHP_VERSION constant holds current PHP version.
Example 1. version_compare() example |
<?php
echo version_compare("4.0.4", "4.0.6");
echo version_compare("4.0.4", "4.0.6", "<");
echo version_compare("4.0.6", "4.0.6", "eq");
?>
|
|
User Contributed Notes
version_compare
angelo [at] mandato <dot> com
05-Nov-2004 01:34
If you upgrade to PHP 5.0 and now receive the following error:
Fatal error: Uncaught exception 'com_exception' with message 'Error The object invoked has disconnected from its clients. '
The function Release() is no longer a member function of the COM object. You will need to comment out or remove the function call from your code.
arnoud at procurios dot nl
29-Sep-2004 04:28
If you're careful, this function actualy works quite nicely for comparing version numbers from programs other than PHP itself. I've used it to compare MySQL version numbers. The only issue is that version_compare doesn't recognize the 'gamma' addition that mysql uses as being later than 'alpha' or 'beta', because the latter two are treated specially. If you keep this in mind though, you should have no problems.
mina86 at tlen dot pl
30-Jun-2004 09:40
Here's a wrapper which is more tolerant as far as order of arguments is considered:
<?php
function ver_cmp($arg1, $arg2 = null, $arg3 = null) {
static $phpversion = null;
if ($phpversion===null) $phpversion = phpversion();
switch (func_num_args()) {
case 1: return version_compare($phpversion, $arg1);
case 2:
if (preg_match('/^[lg][te]|[<>]=?|[!=]?=|eq|ne|<>$/i', $arg1))
return version_compare($phpversion, $arg2, $arg1);
elseif (preg_match('/^[lg][te]|[<>]=?|[!=]?=|eq|ne|<>$/i', $arg2))
return version_compare($phpversion, $arg1, $arg2);
return version_compare($arg1, $arg2);
default:
$ver1 = $arg1;
if (preg_match('/^[lg][te]|[<>]=?|[!=]?=|eq|ne|<>$/i', $arg2))
return version_compare($arg1, $arg3, $arg2);
return version_compare($arg1, $arg2, $arg3);
}
}
?>
It also uses phpversion() as a default version if only one string is present. It can make your code look nicer 'cuz you can now write:
<?php if (ver_cmp($version1, '>=', $version2)) something; ?>
and to check a version string against the PHP's version you might use:
<?php if (ver_cmp('>=', $version)) something; ?>
instead of using phpversion().
aidan at php dot net
26-Jun-2004 05:02
This functionality is now implemented in the PEAR package PHP_Compat.
More information about using this function without upgrading your version of PHP can be found on the below link:
http://pear.php.net/package/PHP_Compat
eric at themepark dot com
21-Jun-2004 11:50
so in a nutshell... I believe it works best like this:
if (version_compare(php_version(), "4.3.0", ">=")) {
// you're on 4.3.0 or later
} else {
// you're not
}
sam at wyvern dot non-spammers-remove dot com dot au
23-May-2004 01:18
Actually, it works to any degree:
<?php
version_compare('1.2.3.4RC7.7', '1.2.3.4RC7.8')
version_compare('8.2.50.4', '8.2.52.6')
?>
will both give -1 (ie the left is lower than the right).
judgej at xaraya dot com
26-May-2003 10:13
The examples given all assume the version numbers consist of exactly three integers. This, however, is not a strict rule.
version_compare() will work correctly when the version numbering goes down to deeper levels, and also when the levels between the two versions being compared differ. So 1.2 will correctly be shown to be a higher version than 1.1.9.8 which in turn is higher than 1.1.9.7.
In addition, if a level contains a non-numeric character, then a string comparison is performed on that level rather than a numeric comparison.
I don't know what the limits are, but it looks to be broad enough to use in most practical situations. I've tested this on PHP 4.1.2.
| |