: This article mainly introduces the comparison function version_compare () in PHP. For more information about PHP tutorials, see. Recently, when I was working on a project, I remembered the php version comparison. I checked baidu and found the version_compare () function.
The version_compare () function is described in the php manual as follows:
Version_compare () is used to compare two "PHP normalization" version numeric strings. This is helpful for compiling programs that are only compatible with some versions of PHP.
This function first uses a vertex in the version string. replace _,-, and +, and insert a vertex before or after any non-number ., in this way, similar to '4. 3.2RC1 'will be changed to '4. 3.2.RC.1 '. Next, it splits the result, just as you used explode ('.', $ ver. Then it compares the parts from left to right. If a part contains a specific version string, it will be processed in the following order: any string not found in the List <dev <alpha = a <beta = B <RC = rc <# <pl = p. This method not only compares different version levels similar to '4. 1' and '4. 1.2 ', but also compares any version that contains the PHP development status.
Mixed version_compare (string$version1
, String$version2
[, String$operator
])
Return hybrid type
String $ version1-version 1 is required
String $ version2-Version 2 is required
String $ operator-optional for operators
That is<,Lt,<=,Le,>,Gt,> =,Ge,=,=,Eq,! =,<>AndNe.
If the third parameter is specified, the return value is boolean. if the third parameter is not specified, the following three conditions are returned:
When the first version is earlier than the second version, return-1
When the first version is equal to the second version, return 0
When the first version is earlier than the second version, return 1
'; // The Current PHP version is '. PHP_VERSION. "\ n"; var_dump (version_compare (PHP_VERSION, '5. 2.0 '); var_dump (version_compare (PHP_VERSION, '5. 2.0 ',' = '); var_dump (version_compare (PHP_VERSION, '5. 3.0 ', 'ge'); if (version_compare (PHP_VERSION, '5. 3.0 ', 'ge') {echo' your PHP version is later than 5.3.0, and the current version is '. PHP_VERSION;} else {echo 'Your PHP version is earlier than 5.3.0, and the current version is '. PHP_VERSION ;}
The result is as follows:
The current PHP version is 5.2.17 int (1) bool (false). Your PHP version is earlier than 5.3.0, and the current version is 5.2.17.
The above describes the comparison function version_compare () in PHP, including the comparison of PHP versions and version_compare. if you are interested in the PHP Tutorial, please help.