Compare two version strings in PHP

compare version numbers in php
php version compare online
if version_compare php_version 5.1 2
php online
if php version greater than
version compare java
version_compare ansible
php dump version

How to compare two strings in version format? such that:

version_compare("2.5.1",  "2.5.2") => -1 (smaller)
version_compare("2.5.2",  "2.5.2") =>  0 (equal)
version_compare("2.5.5",  "2.5.2") =>  1 (bigger)
version_compare("2.5.11", "2.5.2") =>  1 (bigger, eleven is bigger than two)

From the PHP interactive prompt using the version_compare function, built in to PHP since 4.1:

php > print_r(version_compare("2.5.1",  "2.5.2")); // expect -1
-1
php > print_r(version_compare("2.5.2",  "2.5.2")); // expect 0
0
php > print_r(version_compare("2.5.5",  "2.5.2")); // expect 1
1
php > print_r(version_compare("2.5.11", "2.5.2")); // expect 1
1

It seems PHP already works as you expect. If you are encountering different behavior, perhaps you should specify this.

Compare two version strings in PHP, From the PHP interactive prompt using the version_compare function, built in to PHP since 4.1: php > print_r(version_compare("2.5.1", "2.5.2"));  Teams. Q&A for Work. Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information.

Also, you can use the PHP built-in function as below by passing an extra argument to the version_compare()

if(version_compare('2.5.2', '2.5.1', '>')) {
 print "First arg is greater than second arg";
}

Please see version_compare for further queries.

version-compare function, version_compare() compares two "PHP-standardized" version number strings. This is useful if you would like to write programs working only on some versions of  Answer: Use the PHP strcmp() function You can use the PHP strcmp() function to easily compare two strings. This function takes two strings str1 and str2 as parameters. The strcmp() function returns < 0 if str1 is less than str2 ; returns > 0 if str1 is greater than str2, and 0 if they are equal.

If your version compare doesnt work, the code below will produce your results.

function new_version_compare($s1,$s2){
    $sa1 = explode(".",$s1);
    $sa2 = explode(".",$s2);
    if(($sa2[2]-$sa1[2])<0)
        return 1;
    if(($sa2[2]-$sa1[2])==0)
        return 0;
    if(($sa2[2]-$sa1[2])>0)
        return -1;
}

How to Compare Two Strings in PHP, The PHP strcmp() function compare two strings in a case-sensitive manner. If you want case-insensitive comparison, you can use the strcasecmp() function. How To Compare Strings In PHP == operator. The most common way you will see of comparing two strings is simply by using the == operator if the two strcmp Function. Another way to compare strings is to use the PHP function strcmp, this is a binary safe string strcasecmp Function. The previous

I have developed this function. I hope it helps. It can go to any length.

function updateAppVersion($appVersion1, $appVersion2)
{
    $releaseVersion = explode(".",$appVersion1);
    $deviceVersion = explode(".",$appVersion2);

    $len = count($deviceVersion);
    if(count($releaseVersion)>count($deviceVersion)){
        $len = count($releaseVersion);
    }

    for($i = 0;$i<$len;$i++){
        echo "[i=".$i."][r=".$releaseVersion[$i]."][d=".$deviceVersion[$i]."]";
        if(!isset($releaseVersion[$i])){
            return false;
        }
        else if(!isset($deviceVersion[$i])){
            return true;
        }
        else if($releaseVersion[$i]>$deviceVersion[$i]){
            return true;
        }
        else if($releaseVersion[$i]<$deviceVersion[$i]){
            return false;
        }

    }

    return false;
}

PHP strcmp() Function, >0 - if string1 is greater than string2. PHP Version: 4+. More Examples. Example. Compare two strings (case-sensitive = Hello and  0 - if the two strings are equal <0 - if string1 (from startpos) is less than string2 >0 - if string1 (from startpos) is greater than string2; If length is equal or greater than length of string1, this function returns FALSE. PHP Version: 5+ Changelog: As of PHP 5.5.11 - The length parameter can be 0.

what you could do is parse each string, stop at the dot and add each number into a separate int.

so that your string 2.5.1 would become 3 integers:

$ver1 . "." . $ver2 . "." . $ver3

and your string 2.5.11 would become:

$ver1_2 . "." . $ver2_2 . "." . $ver3_2

then a bunch of if's to compare $ver1 with $ver1_2 and so on.

PHP: version_compare, version_compare() compares two "PHP-standardized" version number strings. //Compare two sets of versions, where major/minor/etc. releases are separated  0 - if the two strings are equal <0 - if string1 is less than string2 >0 - if string1 is greater than string2; PHP Version: 4+

Cross-platform code 5: Checking the PHP version with phpversion , The second takes two parameters, which should be two version strings of the that each file is the correct version, you can compare your own version strings  H ow can I compare two strings using php? How do I use php to compare two strings? PHP is an HTML-embedded scripting language. It is an open source project. PHP also offers built-in database integration for several commercial and non-commercial database management systems, so writing a database-enabled webpage with PHP is fairly simple.

How To Compare Strings In PHP, The most common way you will see of comparing two strings is simply by using the == operator if the two strings are equal to each other then it  Compare two Version numbers A version number is a string that is used to identify unique states of a software product. A version number looks like a.b.c.d where a, b, etc are number, so the version number is a string in which numbers are separated by dots.

php:, version_compare() compares two "PHP-standardized" version number strings. //Compare two sets of versions, where major/minor/etc. releases are separated  1) If the two strings have identical BEGINNING parts, they are trunkated from both strings. 2) The resulting strings are compared with two possible outcomes: a) if one of the resulting strings is an empty string, then the length of the non-empty string is returned (the sign depending on the order in which you pass the arguments to the function)

Comments
  • Probably "natural order", have you tried php.net/strnatcmp ?
  • What's wrong with php's builtin version_compare?
  • Test your code before you ask. I might just work (as in your case). -1 for that.
  • @hakre, my fault. I googled all around strcmp, but... It's my fault.
  • Yes sometimes it's not seeing the woods for the trees. It's somehow ironic that you choosed the right function in your question without seeing that ;)
  • Can confirm. Just works. ALL PHP versions. 3v4l.org/U0mvm#v430 Same for the natural order compare.
  • Well that is why I stated if it doesnt work for you, (for some reason it doesnt otherwise he wouldnt ask). But to make sure I edited it to a new_ function