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;
?>
version_compare
(PHP 4 >= 4.0.7, PHP 5)
version_compare — ふたつの "PHP 標準" バージョン番号文字列を比較する
Description
mixed version_compare ( string version1, string version2 [, string operator ] )version_compare()は、ふたつの "PHP 標準" バージョン 番号文字列を比較します。この関数は、いくつかのバージョンの PHP でのみ 動作するプログラムを書きたい場合に有用です。
version_compare()は、最初のバージョンが 2 番目 よりも小さい場合に -1、等しい場合に 0、2 番目が小さい場合に +1 を返し ます。
この関数はまず、バージョン文字列の _, -, + をドット . で置き換えます。 さらに、数値でない部分の前後にドット . を追加します。 例えば '4.3.2RC1' は '4.3.2.RC.1' となります。 次に、explode('.', $ver) とするのと同じように結果を分割し、左から右へ 各部分を比較していきます。特殊な文字列が含まれている場合は以下の順で 並べ替えます: dev < alpha = a < beta = b < RC < pl. この方法により、'4.1' と '4.1.2' のようなバージョンの違いだけではなく PHP 固有の開発ステータスの違いも判断することが可能となります。
3番目のオプション引数 operator を指定した 場合、特定の関係を調べることが可能です。指定可能な演算子を以下に 示します。 <, lt, <=, le, >, gt, >=, ge, ==, =, eq, !=, <>, ne この引数を用いると、この関数はこの演算子により指定された関係が 成り立つ場合に TRUE 、そうでない場合に FALSE を返します。
注意: PHP_VERSION 定数にはカレントの PHP のバージョンが 格納されます。
例 1745. version_compare() の例
<?php
// -1 を表示する
echo version_compare("4.0.4", "4.0.6");
// すべて 1 を表示する
echo version_compare("4.0.4", "4.0.6", "<");
echo version_compare("4.0.6", "4.0.6", "eq");
?>
version_compare
23-Oct-2006 10:38
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.
01-Jul-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().
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
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
}
?>
24-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).