PHP version comparison function version_compare ()

Source: Internet
Author: User

PHP version, generally use version_compare () function, help documentation see: http://php.net/manual/en/function.version-compare.php
Purpose: Compares two "PHP-standardized" version number
Strings.
Syntax:
Version_compare

(String
$ Version1

, String
$ Version2

[, String
$ Operator

])
Description:Version_compare ()

Compares two
"PHP-standardized"
Version number strings. This is useful if you wowould 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.

The third optional parameter is a comparison character:

If you specify the third optionalOperator

Argument, you can test for a particle relationship.
Possible operators are:<
,
Lt
,<=
,
Le
,>
,
GT
,> =
,
Ge
,=
,
=
,EQ
,
! =
,<>
,
Ne
Respectively.

This parameter is case-sensitive, so values shoshould be lowercase.

Return Value:

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 OptionalOperator

Argument,
Function will returnTrue

If the relationship is
One specified
By the operator,False

Otherwise.

The following example shows how to use 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"
;
}

?>

Recently, when installing the Nagios chart monitoring plug-in PNP, an error occurs: Kohana requires PHP 5.2 or newer. Check the page source code and find the following statement:
Version_compare (php_version, '5. 2', '<') and exit ('kohana requires PHP 5.2 or newer .');
That is, exit if the version is not satisfied.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.