Double Decimal Comparison

I want to compare application / software version numbers, which sometimes can have two decimal points, such as:

1.0
1.1
1.0.01
1.0.1
2.0
2.5
3.0

etc .. etc ..

What would be the correct way to compare these numbers?

I tried this, but I get an error:

Parse error: syntax error, unexpected T_DNUMBER at / home / videocoo / public _html / dev / vc-admin / test_cmp.php on line 2

$a = 1.2.11;
$b = 1.2.0;

if($a > $b){
    print"<br />a is greater";
} else {
    print"<br />b is greater";
}

Is it wrong to make numbers in a string, wrapping them in double quotes? It seems that he gave the correct comparison every time I tested different numbers. Thank!

+3
source share
2 answers

The function you are looking for is the version_compare() PHP Reference

<?php
$versionA = '1.0.1';
$versionB = '1.0.2';

if (version_compare($versionA, $versionB) >= 0) {
    echo 'Version B is equal to or greater than Version A';
}

if (version_compare($versionA, $versionB, '<')) {
    echo 'Version A is less than Version B';
}
?>
+5
source
+2

All Articles