php接口版本号比大小

cjh| 阅读:1224 发表时间:2018-03-27 16:47:03 linux
function versionCompare($versionA,$versionB) {
    if ($versionA>2147483646 || $versionB>2147483646) {
        throw new Exception('版本号,位数太大暂不支持!','101');
    }
    $dm = '.';
    $verListA = explode($dm, (string)$versionA);
    $verListB = explode($dm, (string)$versionB);

    $len = max(count($verListA),count($verListB));
    $i = -1;
    while ($i++<$len) {
        $verListA[$i] = intval(@$verListA[$i]);
        if ($verListA[$i] <0 ) {
            $verListA[$i] = 0;
        }
        $verListB[$i] = intval(@$verListB[$i]);
        if ($verListB[$i] <0 ) {
            $verListB[$i] = 0;
        }

        if ($verListA[$i]>$verListB[$i]) {
            return 1;
        } else if ($verListA[$i]<$verListB[$i]) {
            return -1;
        } else if ($i==($len-1)) {
            return 0;
        }
    }

}