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
version_compare
(PHP 4 >= 4.1.0, PHP 5)
version_compare — Vergleicht zwei Versionsnummern im PHP-Stil
Beschreibung
version_compare() vergleicht zwei den PHP-Versionen angeglichenen Versionsnummern. Dies ist beispielsweise nützlich, wenn Code nur unter bestimmten Versionen von PHP funktionieren soll.
Zuerst ersetzt die Funktion _, - und + durch einen Punkt . in den Versionsangaben und setzt vor und nach jeder Kette aus nicht-numerischen Zeichen Punkte ein, sodass beispielsweise '4.3.2RC1' zu '4.3.2.RC.1' wird. Dann wird diese Zeichenkette an den Punkten aufgespalten wie wenn man explode('.', $ver) benutzen würde. Anschließend werden von links nach rechts die Teile verglichen. Wenn ein Teil spezielle Zeichen enthält, werden diese nach der folgenden Reihenfolge behandelt: jede Zeichenkette, die nicht in dieser Liste vorkommt < dev < alpha = a < beta = b < RC = rc < # < pl = p. Auf diese Weise können nicht nur Versionen verschiedener Tiefe wie '4.1' und '4.1.2' sondern auch alle anderen Versionen verglichen werden, die sich auf bestimmte Entwicklungsstadien von PHP beziehen.
Parameter-Liste
-
version1 -
Erste Versionsnummer.
-
version2 -
Zweite Versionsnummer.
-
operator -
Wenn der freiwillige Parameter
operatorangegeben ist, wird auf ein bestimmtes Verhältnis geprüft. Mögliche Operatoren sind: <, lt, <=, le, >, gt, >=, ge, ==, =, eq, !=, <>, ne.Dieser Parameter berücksicht Groß- und Kleinschreibung, die Werte sollten alle kleingeschrieben werden.
Rückgabewerte
Standardmäßig gibt version_compare() -1 zurück, wenn die erste Version kleiner ist als die zweite, 0, wenn die Versionen gleich sind und 1, wenn die zweite Version kleiner ist.
Wenn der optionale Parameter operator übergeben
wurde, gibt die Funktion TRUE oder FALSE zurück, je nach dem, ob
das mit dem Operator definierte Verhältnis der Wahrheit entspricht
oder nicht.
Beispiele
Das untenstehende Beispiel benutzt die Konstante PHP_VERSION,
die die Version des aktuell ausführenden PHP-Interpreters enthält.
Beispiel #1 version_compare()-Beispiele
<?php
if (version_compare(PHP_VERSION, '6.0.0') >= 0) {
echo 'Ich bin mindestens PHP 6.0.0, und zwar: ' . PHP_VERSION . "\n";
}
if (version_compare(PHP_VERSION, '5.3.0') >= 0) {
echo 'Ich bin mindestens PHP 5.3.0, nämlich: ' . PHP_VERSION . "\n";
}
if (version_compare(PHP_VERSION, '5.0.0', '>=')) {
echo 'Ich bin PHP 5. Meine Versionsnummer lautet: ' . PHP_VERSION . "\n";
}
if (version_compare(PHP_VERSION, '5.0.0', '<')) {
echo 'Ich bin PHP 4. Meine Versionsnummer lautet: ' . PHP_VERSION . "\n";
}
?>
Anmerkungen
Hinweis:
Die Konstante
PHP_VERSIONenthält die aktuelle PHP-Version
Hinweis:
Vorveröffentlichte Versionen wie 5.3.0-dev werden als kleiner erkannt als ihre finalen Veröffentlichungen wie 5.3.0.
Siehe auch
- phpversion() - Liefert die aktuelle PHP-Version
- php_uname() - Returns information about the operating system PHP is running on
- function_exists() - Falls die angegebene Funktion definiert ist, wird TRUE zurück gegeben
version_compare
11-Jun-2007 10:26
11-Jun-2007 03: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.
11-Jun-2007 02: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', '>')
23-Oct-2006 03: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;
?>
29-Sep-2004 11: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.
01-Jul-2004 04: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().
26-Jun-2004 12: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
22-Jun-2004 06: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
}
?>
23-May-2004 08: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).