PHP
downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

zend_logo_guid> <sys_get_temp_dir
Last updated: Fri, 27 Jun 2008

view this page in

version_compare

(PHP 4 >= 4.0.7, PHP 5)

version_compare — Compares two "PHP-standardized" version number strings

Description

mixed version_compare ( string $version1 , string $version2 [, string $operator ] )

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 PHP.

The function first replaces _, - and + with a dot . in the version strings and also inserts dots . before and after any non number so that for example '4.3.2RC1' becomes '4.3.2.RC.1'. Then it splits the results like if you were using explode('.', $ver). Then it compares the parts starting from left to right. If a part contains special version strings these are handled in the following order: dev < alpha = a < beta = b < RC < pl. This way not only versions with different levels like '4.1' and '4.1.2' can be compared but also any PHP specific version containing development state.

Parameters

version1

First version number.

version2

Second version number.

operator

If you specify the third optional operator argument, you can test for a particular relationship. The possible operators are: <, lt, <=, le, >, gt, >=, ge, ==, =, eq, !=, <>, ne respectively.

This parameter is case-sensitive, so values should be lowercase.

Return Values

By default, version_compare() returns -1 if the first version is lower than the second, 0 if they are equal, and 1 if the second is lower.

When using the optional operator argument, the function will return TRUE if the relationship is the one specified by the operator, FALSE otherwise.

Examples

The examples below use the PHP_VERSION constant, because it contains the value of the PHP version that is executing the code.

Example #1 version_compare() examples

<?php
if (version_compare(PHP_VERSION'6.0.0') === 1) {
    echo 
'I am at least PHP version 6.0.0, my version: ' PHP_VERSION "\n";
}

if (
version_compare(PHP_VERSION'5.3.0') === 1) {
    echo 
'I am at least PHP version 5.3.0, my version: ' PHP_VERSION "\n";
}

if (
version_compare(PHP_VERSION'5.0.0''>')) {
    echo 
'I am using PHP 5, my version: ' PHP_VERSION "\n";
}

if (
version_compare(PHP_VERSION'5.0.0''<')) {
    echo 
'I am using PHP 4, my version: ' PHP_VERSION "\n";
}
?>

Notes

Note: The PHP_VERSION constant holds current PHP version.

Note: Note that pre-release versions, such as 5.3.0-dev, are considered lower than their final release counterparts (like 5.3.0).



zend_logo_guid> <sys_get_temp_dir
Last updated: Fri, 27 Jun 2008
 
add a note add a note User Contributed Notes
version_compare
bishop
07-Mar-2008 03:54
<?php
// quick & dirty way to barricade your code during version transitions
assert('version_compare("5", PHP_VERSION, "<"); // requires PHP 5 or higher');
?>
Rickard Andersson
30-Oct-2007 06:18
It should be noted that version_compare() considers 1 < 1.0 < 1.0.0 etc. I'm guessing this is due to the left-to-right nature of the algorithm.
Jonathon dot Reinhart at gmail dot com
30-Oct-2007 12:38
I know this is somewhat incomplete, but it did a fair enough job for what I needed.  I was writing some code that needed done immediately on a server that was to be upgraded some time in the future.  Here is a quick replacement for version_compare (without the use of the operator argument). Feel free to add to this / complete it.

<?php
function version_compare2($version1, $version2)
{
   
$v1 = explode('.',$version1);
   
$v2 = explode('.',$version2);
   
    if (
$v1[0] > $v2[0])
       
$ret = 1;
    else if (
$v1[0] < $v2[0])
       
$ret = -1;
   
    else   
// Major ver are =
   
{
        if (
$v1[1] > $v2[1])
           
$ret = 1;
        else if (
$v1[1] < $v2[1])
           
$ret = -1;
       
        else 
// Minor ver are =
       
{
            if (
$v1[2] > $v2[2])
               
$ret = 1;
            else if (
$v1[2] < $v2[2])
               
$ret = -1;
            else
               
$ret = 0;
        }
    }
   
    return
$ret;
}
?>
Hayley Watson
31-Jul-2007 02:31
Yes; I was incorrect. However "1.0pl" is greater than "1.0.1", since "pl" comes after any number (this should perhaps be noted in the documentation: dev < alpha = a < beta = b < RC < # < pl)
opendb at iamvegan dot net
11-Jun-2007 05:26
My concern at this point, is that Hayley Watson's comments may confuse some people - it did me, so I did some more investigation before replying...

According to all the unit tests (15) I have written using version_compare, I believe Hayley Watson is incorrect.  For example, all of these calls are true.

function testVersionBeta3ToBeta4()
{
    $this->assertTrue(opendb_version_compare('1.0b3', '1.0a4', '>'), '1.0b3 > 1.0a4');
}

function testVersionBeta6ToReleaseCandidate1()
{
    $this->assertTrue(opendb_version_compare('1.0RC1', '1.0b6', '>'), 'RC1 > 1.0b6');
}

So how is this any different (which does not return true)

function testVersion10pl1To101()
{
        $this->assertTrue(opendb_version_compare('1.0.1', '1.0pl1', '>'), '1.0.1 > 1.0pl1');
}

However when its changed to:

unction testVersion10pl1To101()
{
        $this->assertTrue(opendb_version_compare('1.0.1', '1.0pl1', '>'), '1.0.1 > 1.0.0pl1');
}

It works fine.  I do believe that the version_compare function works as follows:

$version1 $operator $version2
Hayley Watson
10-Jun-2007 10:47
To correct opendb at iamvegan dot net's misapprehension: "1.0pl1" is regarded as less than "1.0.1". But the call as you've written it asks whether "1.0pl1" is greater (">") than "1.0.1" - which it isn't.
opendb at iamvegan dot net
10-Jun-2007 09:01
Something that may trip some folks up, but is useful to mention is that the following version comparison does not work quite as I expected:
    version_compare('1.0.1', '1.0pl1', '>')

However, its quite easy to get working:
    version_compare('1.0.1', '1.0.0pl1', '>')
MagicalTux at ookoo dot org
23-Oct-2006 10:38
To answer elmuerte's note (06-Jul-2006 03:24), you'd even better remove spaces than replacing them.

<?php
version_compare
("1.0.0.0beta1", "1.0.0.0alpha2") == 1; // good
version_compare("1.0.0.0-beta 1", "1.0.0.0-alpha 2") == 1;
version_compare("1.0.0.0.beta 1", "1.0.0.0.alpha 2") == 1;
version_compare("1.0.0.0.beta.1", "1.0.0.0.alpha.2") == 1;
?>
arnoud at procurios dot nl
29-Sep-2004 06:28
If you're careful, this function actualy works quite nicely for comparing version numbers from programs other than PHP itself. I've used it to compare MySQL version numbers. The only issue is that version_compare doesn't recognize the 'gamma' addition that mysql uses as being later than 'alpha' or 'beta', because the latter two are treated specially. If you keep this in mind though, you should have no problems.
mina86 at tlen dot pl
30-Jun-2004 11:40
Here's a wrapper which is more tolerant as far as order of arguments is considered:

<?php
function ver_cmp($arg1, $arg2 = null, $arg3 = null) {
  static
$phpversion = null;
  if (
$phpversion===null) $phpversion = phpversion();

  switch (
func_num_args()) {
  case
1: return version_compare($phpversion, $arg1);
  case
2:
    if (
preg_match('/^[lg][te]|[<>]=?|[!=]?=|eq|ne|<>$/i', $arg1))
      return
version_compare($phpversion, $arg2, $arg1);
    elseif (
preg_match('/^[lg][te]|[<>]=?|[!=]?=|eq|ne|<>$/i', $arg2))
      return
version_compare($phpversion, $arg1, $arg2);
    return
version_compare($arg1, $arg2);
  default:
   
$ver1 = $arg1;
    if (
preg_match('/^[lg][te]|[<>]=?|[!=]?=|eq|ne|<>$/i', $arg2))
      return
version_compare($arg1, $arg3, $arg2);
    return
version_compare($arg1, $arg2, $arg3);
  }
}
?>

It also uses phpversion() as a default version if only one string is present. It can make your code look nicer 'cuz you can now write:
<?php if (ver_cmp($version1, '>=', $version2)) something; ?>
and to check a version string against the PHP's version you might use:
<?php if (ver_cmp('>=', $version)) something; ?>
instead of using phpversion().
aidan at php dot net
26-Jun-2004 07:02
This functionality is now implemented in the PEAR package PHP_Compat.

More information about using this function without upgrading your version of PHP can be found on the below link:

http://pear.php.net/package/PHP_Compat
eric at themepark dot com
22-Jun-2004 01:50
[editors note]
snipbit fixed after comment from Matt Mullenweg

--jm
[/editors note]

so in a nutshell... I believe it works best like this:

<?php
if (version_compare(phpversion(), "4.3.0", ">=")) {
 
// you're on 4.3.0 or later
} else {
 
// you're not
}
?>
sam at wyvern dot non-spammers-remove dot com dot au
23-May-2004 03:18
Actually, it works to any degree:

<?php
version_compare
('1.2.3.4RC7.7', '1.2.3.4RC7.8')
version_compare('8.2.50.4', '8.2.52.6')
?>

will both give -1 (ie the left is lower than the right).

zend_logo_guid> <sys_get_temp_dir
Last updated: Fri, 27 Jun 2008
 
 
show source | credits | sitemap | contact | advertising | mirror sites