(PHP 4 >= 4.1.0, PHP 5)
version_compare — 对比两个「PHP 规范化」的版本数字字符串
version_compare() 用于对比两个「PHP 规范化」的版本数字字符串。 这对于编写仅能兼容某些版本 PHP 的程序很有帮助。
此函数首先在版本字符串里用一个点 . 替换 _、- 和 +,也会在任意非数字前后插入一个点 .,这样,类似 '4.3.2RC1' 将会变成 '4.3.2.RC.1'。 接下来它会分割结果,就像你使用 explode('.', $ver) 那样。 然后它会从左往右对比各个部分。 如果某部分包含了特定的版本字符串,将会用以下顺序处理: 列表中未找到的任意字符串 < dev < alpha = a < beta = b < RC = rc < # < pl = p。 这种方式不仅能够对比类似 '4.1' 和 '4.1.2' 那种不同的版本级别,同时也可以指定对比任何包含 PHP 开发状态的版本。
version1
第一个版本数。
version2
第二个版本数。
operator
如果你指定了可选的第三个参数 operator
,你可以测试两者的特定关系。
可以的操作符分别是:<、
lt、<=、
le、>、
gt、>=、
ge、==、
=、eq、
!=、<> 和
ne。
此参数区分大小写,所以它的值应该是小写的。
默认情况下,在第一个版本低于第二个时, version_compare() 返回 -1;如果两者相等,返回 0;第二个版本更低时则返回 1。
当使用了可选参数 operator
时,如果关系是操作符所指定的那个,函数将返回 TRUE
,否则返回 FALSE
。
下例使用了 PHP_VERSION
常量,因为它执行的代码包含了 PHP 版本的值。
Example #1 version_compare() examples
<?php
if (version_compare(PHP_VERSION, '6.0.0') >= 0) {
echo 'I am at least PHP version 6.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 using PHP 5, my version: ' . PHP_VERSION . "\n";
}
if (version_compare(PHP_VERSION, '5.0.0', '<')) {
echo 'I am using PHP 4, my version: ' . PHP_VERSION . "\n";
}
?>
Note:
PHP_VERSION
常量包含了当前 PHP 的版本。
Note:
注意,类似 5.3.0-dev 的预发行版本,被认为是低于它们的最终发行版本(就像 5.3.0)。
Note:
指定类似 alpha、beta 的版本字符串是大小写敏感的。 版本字符串的来源若不遵循 PHP 标准,可能需要在调用 version_compare() 之前先用 strtolower() 转成小写。
mindplay.dk (2012-07-21 03:10:36)
This little script can perhaps help you understand version comparison a little better - the output is displayed in the comment at the top. Tweak the list of versions if you need more examples...
<?php
# 1 lt 1.0
# 1.0 lt 1.01
# 1.01 eq 1.1
# 1.1 lt 1.10
# 1.10 gt 1.10b
# 1.10b lt 1.10.0
header('Content-type: text/plain');
$versions = array(
'1',
'1.0',
'1.01',
'1.1',
'1.10',
'1.10b',
'1.10.0',
);
$comps = array(
-1 => 'lt',
0 => 'eq',
1 => 'gt'
);
foreach ($versions as $version) {
if (isset($last)) {
$comp = version_compare($last, $version);
echo str_pad($last,8,' ',STR_PAD_LEFT) . " {$comps[$comp]} {$version}\n";
}
$last = $version;
}
?>
rogier (2011-11-30 08:43:40)
Please note that supplying an operator that is not listed (e.g. ===), this function returns NULL instead of false.
Tested on PHP5.3.0, Win32
Steve Kamerman (2011-06-24 12:07:47)
I know I'm splitting hairs here, but if you're OCD like me
(version_compare(PHP_VERSION, '5.3.0') >= 0)
is about 13% faster than
(version_compare(PHP_VERSION, '5.3.0', '>=')
<?php
$count = 200000;
$start = microtime(true);
for($i=0;$i<$count;$i++) (version_compare(PHP_VERSION, '5.3.0') >= 0);
echo "Real Operator: ".(microtime(true) - $start)."\n";
$start = microtime(true);
for($i=0;$i<$count;$i++) (version_compare(PHP_VERSION, '5.3.0', '>='));
echo "String Operator: ".(microtime(true) - $start)."\n";
?>
loaded67 at hotmail dot com (2009-09-30 04:53:33)
This function is also usefull when working with multiple installations.
As php5.3+ will not have E_STRICT in the error_reporting anymore you can state:
<?php
ini_set('error_reporting', (version_compare(PHP_VERSION, '5.3.0', '<') ? E_ALL|E_STRICT : E_ALL));
?>
Giving you all the error error reporting you want...
Sina Salek (2009-08-15 02:47:28)
Sometimes the code is forward compatible, for example when the code is compatible with all future PHP5 releases.
This function supports .x, for the above example it's : 5.x
<?php
function versionCompare($version1,$version2,$operand) {
$v1Parts=explode('.',$version1);
$version1.=str_repeat('.0',3-count($v1Parts));
$v2Parts=explode('.',$version2);
$version2.=str_repeat('.0',3-count($v2Parts));
$version1=str_replace('.x','.1000',$version1);
$version2=str_replace('.x','.1000',$version2);
return version_compare($version1,$version2,$operand);
}
?>
---
Sina Salek
http://sina.salek.ws/en/contact
insid0r at yahoo dot com (2009-03-06 13:05:42)
Since this function considers 1 < 1.0 < 1.0.0, others might find this function useful (which considers 1 == 1.0):
<?php
//Compare two sets of versions, where major/minor/etc. releases are separated by dots.
//Returns 0 if both are equal, 1 if A > B, and -1 if B < A.
function version_compare2($a, $b)
{
$a = explode(".", rtrim($a, ".0")); //Split version into pieces and remove trailing .0
$b = explode(".", rtrim($b, ".0")); //Split version into pieces and remove trailing .0
foreach ($a as $depth => $aVal)
{ //Iterate over each piece of A
if (isset($b[$depth]))
{ //If B matches A to this depth, compare the values
if ($aVal > $b[$depth]) return 1; //Return A > B
else if ($aVal < $b[$depth]) return -1; //Return B > A
//An equal result is inconclusive at this point
}
else
{ //If B does not match A to this depth, then A comes after B in sort order
return 1; //so return A > B
}
}
//At this point, we know that to the depth that A and B extend to, they are equivalent.
//Either the loop ended because A is shorter than B, or both are equal.
return (count($a) < count($b)) ? -1 : 0;
}
?>
bishop (2008-03-07 10:54:38)
<?php
// quick & dirty way to barricade your code during version transitions
assert('version_compare("5", PHP_VERSION, "<"); // requires PHP 5 or higher');
?>
Rickard Andersson (2007-10-30 13:18:32)
It should be noted that version_compare() considers 1 < 1.0 < 1.0.0 etc. I'm guessing this is due to the left-to-right nature of the algorithm.
Jonathon dot Reinhart at gmail dot com (2007-10-30 07:38:57)
I know this is somewhat incomplete, but it did a fair enough job for what I needed. I was writing some code that needed done immediately on a server that was to be upgraded some time in the future. Here is a quick replacement for version_compare (without the use of the operator argument). Feel free to add to this / complete it.
<?php
function version_compare2($version1, $version2)
{
$v1 = explode('.',$version1);
$v2 = explode('.',$version2);
if ($v1[0] > $v2[0])
$ret = 1;
else if ($v1[0] < $v2[0])
$ret = -1;
else // Major ver are =
{
if ($v1[1] > $v2[1])
$ret = 1;
else if ($v1[1] < $v2[1])
$ret = -1;
else // Minor ver are =
{
if ($v1[2] > $v2[2])
$ret = 1;
else if ($v1[2] < $v2[2])
$ret = -1;
else
$ret = 0;
}
}
return $ret;
}
?>
opendb at iamvegan dot net (2007-06-10 17:01:08)
Something that may trip some folks up, but is useful to mention is that the following version comparison does not work quite as I expected:
version_compare('1.0.1', '1.0pl1', '>')
However, its quite easy to get working:
version_compare('1.0.1', '1.0.0pl1', '>')
arnoud at procurios dot nl (2004-09-29 02:28:06)
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 (2004-06-30 19:40:41)
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().
eric at themepark dot com (2004-06-21 21:50:21)
[editors note]
snipbit fixed after comment from Matt Mullenweg
--jm
[/editors note]
so in a nutshell... I believe it works best like this:
<?php
if (version_compare(phpversion(), "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 (2004-05-23 11:18:28)
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).