首页 >函数列表 >version_compare

version_compare

version_compare

(PHP 4 >= 4.1.0, PHP 5)

version_compareCompares two "PHP-standardized" version number strings

说明

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: any string not found in this list < dev < alpha = a < beta = b < RC = rc < # < pl = p. 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.

参数

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.

返回值

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.

范例

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') >= 0) {
    echo 
'I am at least PHP version 6.0.0, my version: ' PHP_VERSION " ";
}

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

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

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

注释

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

参见


  • assert
  • assert_options
  • dl
  • extension_loaded
  • gc_collect_cycles
  • gc_disable
  • gc_enable
  • gc_enabled
  • getenv
  • getlastmod
  • getmyinode
  • getopt
  • getrusage
  • get_cfg_var
  • get_current_user
  • get_defined_constants
  • get_extension_funcs
  • get_included_files
  • get_include_path
  • get_loaded_extensions
  • get_magic_quotes_gpc
  • get_magic_quotes_runtime
  • get_required_files
  • ini_alter
  • ini_get
  • ini_get_all
  • ini_restore
  • ini_set
  • magic_quotes_runtime
  • main
  • memory_get_peak_usage
  • memory_get_usage
  • phpcredits
  • phpversion
  • php_ini_loaded_file
  • php_ini_scanned_files
  • php_logo_guid
  • php_sapi_name
  • php_uname
  • 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
  • PHP MySQL HTML CSS JavaScript MSSQL AJAX .NET JSP Linux Mac ASP 服务器 SQL jQuery C# C++ java Android IOS oracle MongoDB SQLite wamp 交通频道