<?php |
/** |
* 检测最新版本 |
*/ |
function isSecondBigger( $first , $second ) { |
//纯数字比较 |
if ( is_numeric ( $first ) && is_numeric ( $second )) { |
if ( $second > $first ) { |
return true; |
} |
else { |
return false; |
} |
} |
//x.x.x.x比较 |
if ( stripos ( $second , '.' ) !== false) { |
$f = explode ( '.' , $first ); |
$s = explode ( '.' , $second ); |
$count = count ( $f ); |
foreach ( $f as $k => $v ) { |
//比如1.0比2.0 |
if ( $s [ $k ] > $v ) { |
return true; |
} |
//前面几位相等没关系,最后一位必须大于 |
//比如1.0.5比1.0.8 |
if (( $count == $k +1) && ( $s [ $k ] > $v )) { |
return true; |
} |
} |
} |
return false; |
} |
$v = '1.0.5' ; //用户版本 |
$latest = '1.0.8' ; //最新版本 |
$is_need_update = false; |
if (isSecondBigger( $v , $latest )) { |
$is_need_update = true; |
} |
var_dump( $is_need_update ); |
?> |