Home > Article > Backend Development > PHP version judgment version_compare() function, phpversion_compare_PHP tutorial
Brief search in Du Niang to determine whether the current PHP version is higher than a certain version, or lower than a certain version A version of the method
The displayed results are basically the same. Well, if I hadn’t forgotten the version_compare() function, I wouldn’t have gone to Du Niang and decided to look for the previous code
This is how the version_compare() function is described in the PHP manual:
version_compare() is used to compare two "PHP normalized" version number strings. This is helpful for writing programs that are only compatible with certain versions of PHP
mixed version_compare ( string $version1
, string $version2
[, string $operator
] )
Return to hybrid
string $version1 - version 1 required
string $version2 - version 2 required
string $operator - understand it as an operator. Optional
That is, 7bcfb6ad8c627ab234ec8d6a299cc258, gt, >=, ge, ==, =, eq, !=, a8093152e673feb7aba1828c43532094 and ne.
If the third parameter is specified, boolean will be returned. If the third parameter is not specified, the following three situations will be returned:
When the first version is lower than the second version return -1
When the first version is equal to the second version, return 0
When the first version is smaller than the second version, return 1
<?php header('content-type:text/html;charset=utf-8'); /** * 判断php的版本是否在5.3.0以上 */ echo '<pre class="brush:php;toolbar:false">'; //本人使用的版本为 5.2.17 echo 'PHP的当前版本为 '.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 '您的PHP版本大于5.3.0,当前版本为 '.PHP_VERSION; }else{ echo '您的PHP版本小于5.3.0,当前版本为 '.PHP_VERSION; }
The results are as follows:
PHP的当前版本为 <span>5.2</span>.<span>17</span><span> int(</span><span>1</span><span>) bool(</span><span>false</span><span>) bool(</span><span>false</span><span>) 您的PHP版本小于5.</span><span>3.0</span>,当前版本为 <span>5.2</span>.<span>17</span>