|
version_compare
Compares two "PHP-standardized" version number strings
(PHP 4 >= 4.0.7, PHP 5)
Example 1861. version_compare() example<?php Code Examples / Notes » version_compareeric
[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 } ?> hayley watson
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)
magicaltux
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; ?> aidan
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 opendb
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', '>') rickard andersson
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.
arnoud
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.
jonathon dot reinhart
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; } ?> mina86
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(). sam
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). |
Change Languageassert_options assert dl extension_loaded get_cfg_var get_current_user get_defined_constants get_extension_funcs get_include_path get_included_files get_loaded_extensions get_magic_quotes_gpc get_magic_quotes_runtime get_required_files getenv getlastmod getmygid getmyinode getmypid getmyuid getopt getrusage ini_alter ini_get_all ini_get ini_restore ini_set main memory_get_peak_usage memory_get_usage php_ini_scanned_files php_logo_guid php_sapi_name php_uname phpcredits phpinfo phpversion putenv restore_include_path set_include_path set_magic_quotes_runtime set_time_limit sys_get_temp_dir version_compare zend_logo_guid zend_thread_id zend_version |