note 43710 added to function.version-compare
| From: | mina86 at tlen dot pl | Date: | Wed, 30 Jun 2004 23:40:41 +0000 |
| Subject: | note 43710 added to function.version-compare | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-72204@lists.php.net to get a copy of this message | ||
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().
----
Manual Page -- http://www.php.net/manual/en/function.version-compare.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+43710
Delete -- http://master.php.net/manage/user-notes.php?action=delete+43710&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+43710&report=yes
Search -- http://master.php.net/manage/user-notes.php