php版本比較函數version_compare()

來源:互聯網
上載者:User

php中比較php版本,一般用version_compare()函數,協助文檔見:http://php.net/manual/en/function.version-compare.php
用途:Compares two "PHP-standardized" version number
strings。
文法:
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.

    其中第三個選擇性參數是比較符:

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.

    從下面的例子,就可以很好地說明version_compare的用法:


<?php

if (
version_compare
(
PHP_VERSION

'6.0.0'
) >= 
0
) {
    echo 
'I am at least PHP version 6.0.0, my version: ' 

PHP_VERSION 

"/n"
;
}

if (
version_compare
(
PHP_VERSION

'5.3.0'
) >= 
0
) {
    echo 
'I am at least PHP version 5.3.0, my version: ' 

PHP_VERSION 

"/n"
;
}

if (
version_compare
(
PHP_VERSION

'5.0.0'

'>='
)) {
    echo 
'I am using PHP 5, my version: ' 

PHP_VERSION 

"/n"
;
}

if (
version_compare
(
PHP_VERSION

'5.0.0'

'<'
)) {
    echo 
'I am using PHP 4, my version: ' 

PHP_VERSION 

"/n"
;
}

?>

    最近在nagios的圖表監控外掛程式pnp安裝中遇到一個錯誤提示:Kohana requires PHP 5.2 or newer.查看頁面源碼,發現如下語句:
version_compare(PHP_VERSION, '5.2', '<') and exit('Kohana requires PHP 5.2 or newer.');
也就是版本不滿足就退出。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.