Coding Tips -- Compare The Version Numbers Between Two Releases
Given two version numbers, How to check which release is newer ? For example:
1.1 is newer than 1.0 (1.1 > 1.0);
1.0.0 is equal to 1.0(1.0.0 = 1.0);
0.9 is older than 1.0(0.9 < 1.0).
Function implement is as below:
/**
* Compare two versions.
* @param string $newVersion
* @param string $oldVersion
* @return int. 1 represents '>',0 represents '=',-1 represents '<'
*/
function versionCompare($newVersion,$oldVersion)
{
$newArray = explode(".",$newVersion);
$oldArray = explode(".",$oldVersion);
do{
$v1 = array_shift($newArray);
$v2 = array_shift($oldArray);
if($v1 == NULL){
$v1 = 0;
}
if($v2 == NULL){
$v2 = 0;
}
if($v1 > $v2){
return 1;
}else if($v1 < $v2){
return -1;
}
}while(count($newArray) > 0 || count($oldArray) > 0);
return 0;
}
Test Cases is as below:
var_dump(versionCompare('','')); // =
var_dump(versionCompare('1.0','1.0')); // =
var_dump(versionCompare('1.1','1.0')); // >
var_dump(versionCompare('0.9','1.0')); // <
var_dump(versionCompare('1.0.0','1.0')); // =
var_dump(versionCompare('1.0.1','1.0')); // >
var_dump(versionCompare('1.0.1','1.0.2')); // <
Coding Tips -- Compare The Version Numbers Between Two Releases
https://blog.weixinbook.net/2014/12/20/version-compare-function.html
# 相关文章
1.PHP从数组中删除元素的四种方法
2.PHP异步任务队列管理器asyntask介绍
3.利用中文分词正向最大匹配法解决自动提取标签问题
4.深夜中消失的广告位
5.HTTP 416错误与断点续传
6.PHP扩展开发Demo
1.PHP从数组中删除元素的四种方法
2.PHP异步任务队列管理器asyntask介绍
3.利用中文分词正向最大匹配法解决自动提取标签问题
4.深夜中消失的广告位
5.HTTP 416错误与断点续传
6.PHP扩展开发Demo