注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

让一切都逝去吧

排骨炖泥菜/猪肝炒苹果/鱼籽狗肉汤/狗头薏米汤

 
 
 

日志

 
 

php版本比较函数version_compare()  

2010-07-05 17:32:16|  分类: php |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
    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.');
也就是版本不满足就退出。

   
strcmp()的字符串比较函数,也可以进行比较,但像下面这样,就会出N钡sp;usind文章分享到;">'I am using PHP 4, my&nbs瑀at伪祇lor: rd Pcomr/div>
1e 正确Pcomw-blogult, w-blogw-blog-se ssssss荆话阌胿erbsp;  "mw-blog憧纯" cssssb(0, sharw-blog-sfc11 nbw-blog w-blog-sfcarw> , edI s0 bdc0"> "> w-blog-sarw-blog-sfc11 nbw-blog ztag">&np="r="pri &np="bspfc11 nbw-bssssbo&nbstag">&n

bcmom_i);">'I&觯伪祍pfc11 nbw-bssssbo&nbstag">&nnbc-0)函c-0-40 p="mt p="mt- 评论这张觯伪祍pfc11 nbw-bss觯伪祍pfc11 nbw-bw-blog-sarwog zx;text-decoration:n &np="bspfc11 nbw-bssssbo&nbstag">&nnbc-0)函c-0-40 p="mispfc11 nbw-bssss yx"blog/static/3.bst.126P-sta<reg.1 }elss/mi 38380png?rge/pfc11 nbw-bssssbpan>&nnbc-0)函c-0-40 p="msp;转发至v id觯伪祍pfc11 nbw-b w-错误蘤c11 nbw-bw-blog-sssssssss荆话阌胿erp="r="prispfc11 nbw-bss掌琻s">iantashtoeibo2" 'I&觯 &np="bspfc11 nbw-bssssbo&nbstag">&nnbc-0)函c-0-40 p="mispfc11 nbw-bssss yx"blog/static/3.bst.126P-sta<reg.1 }elss/mi 38380png?rge/pfc11 nbw-bssssbpan>&nnbc-0)函c-0-40 p="mtp;转发至v id觯伪祍pfc11 nbw-b w-错误蘤c11 nbw-bw-blog-s憧纯" cssssb(0,ztag">&n t bdn>&neer.op bdwzx;text-f="http3ype= &n3 m2">="it(-bkicons">&nbgb(iRiadClt;sp;an 8 ] 错误蘰 class=緊&nbstag">&n cla3 m2"> &n3 m2">评论(-bkicons">&nbgb(ispa/p>&npled i/b>fspfc11 nbw-bdip p 緊&nbsx;text-decoration:n &n享到LOFTER" class="shareitm lofter f-bkicons">       

用易信  “扫一扫”

1 荆话阌胿er脚笥讶Α

 
i source=%E5%8D%9&nbr_m1> 4gefc11 nssssssssssssbinp

age.js000den" n>&nsespartIspanr>age.js000den" n>&nse hanpanr>age.js000den" n>&nse panr>age.js000den" n>&nse问p>用途:Compares two "PHP-standardized" version number strings。ompare str退出br到一穝pan>退出 错误到一退出错误到一退出b到一.2 or newer.');退出 b到一退出 错误到一ult, ( 退出错误到一 退出错误到一o that退出 错误到一 退出tt到一span> 退出 tt到一退出 错误到一ult,, 退出错误到一 退出错误到一o that退出 错误到一 退出tt到一span> 2退出 tt到一退出 错误到一ult,ompares' can b The function first replaces _, - and + with 退出p到一ult,Tand i> in the version st退出i到一_退出/i到一 before an退出i到一.退出/i到一tny non number so that for example '4.3.2RC1' 退出i到一.退出/i到一t4.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 fouht pan>sr> < dev < alpha<退出i到一 = a < beta =退出/i到一tv cla顺

退出i到一:<退出/i到一tv cla顺 退出i到一 退出/i到一t> <退出i到一 退出/i到一tv cla顺 退出i到一 <退出i到一b退出/i到一tv cla顺 退出i到一RC退出/i到一t=ly ver退出i到一rc退出/i到一tv cla顺 退出i到一#退出/i到一tv cla顺鰑lt,退出i到一pl退出/i到一t=,退出i到一p退出/i到一' can be compared but also any PHP specific version containing development state.

    其中第三个可选参数是比较符:

退出br到一退出/p到一退出p到一蚷f ;bsp;amif ;bsp;av clabsp;a fy the third optional <, lt, <=, le, >,退出i到一蚷f ;顺鐾顺/i到一    返回值:

By default, ret退出br到一退出/dt到一v顺/dl到一退出p到一ult,the first ve退出错误到一退出b到一.2 or newer.');(n退出/b到一退出 错误到一e>0 if th退出i到一-1退出/i到一ual, and 1 if the second is lower.

退出i到一0退出/i到一ual, anusing the optional <退出i到一1退出/i到一ual, andtt>
argument, t退出/p到一ult退出p到一ult will return TRUE退出i到一退出tt到一下面的例退出 tt到一退出 i到一nship is the one specified by the op退出b到一退出tt到一 退出, <到一退出/b到一ual, and> otherwise.

    从下面的例子退出b到一退出tt到一院芎猛顺, <到一退出/b到一uion_compare退出br到一退出,p到一退出p到一蚷f ;bsp;amif ;bsp;av clabsp;a span style="color: rgb(0, 0, 0);"> if (if (if (">versio"到一if (pnp安装"到一.2 or newer.');退出,错误到一退出错误ht 119,"0);">if (">versio"到一(退出,错误到一退出错误ht 119,"0);">if (pnp安装"到一br>也就是版退出,错误到一退出错误ht 119,"0);">if (">versio"到一,pif ;bsp;a退出,错误到一退出错误ht 119,"0);">if&nbsan>if np皃an>if nppnp安装"到一0退出/错误到一退出错误ht 119,"0);">if (皃an>if&nbsan>Ipif ;bsp;an:&if ;bsp;a'&nif ;bsp;a/span>if ;bsp;abr>nif ;bsp;a.2 or nnif ;bsp;a119, 0)if ;bsp;anbsif ;bsp;a.2 or n:sif ;bsp;a'sif ;bsp;a退出,错误到一退出错误ht 119,"0);">if np皃an>if nppnp安装"到一br>也就是版蚷f ;bsp;a退出,错误到一退出错误ht 119,"0);">if np皃an>if&nbsan>if np皃an>if (pnp安装"到一.2 or newer.');退出,错误到一退出错误ht 119,"0);">if (">versio"到一(退出,错误到一退出错误ht 119,"0);">if (pnp安装"到一br>也就是版退出,错误到一退出错误ht 119,"0);">if (皃an>if&nbsan>if np皃an>if nppnp安装"到一0退出/错误到一退出错误ht 119,"0);">if (皃an>if&nbsan>if ;bsp;abr>nif ;bsp;a.2 or nnif ;bsp;a119, 0)if ;bsp;anbsif ;bsp;a.2 or n:sif ;bsp;a'sif ;bsp;a退出,错误到一退出错误ht 119,"0);">if np皃an>if nppnp安装"到一br>也就是版蚷f ;bsp;a退出,错误到一退出错误ht 119,"0);">if np皃an>/sp"到一.sif ;bsp;a退出,错误到一退出错误ht 119,"0);">if&nbsan>if np皃an>if (pnp安装"到一.2 or newer.');退出,错误到一退出错误ht 119,"0);">if (">ve"col"到一(退出,错误到一退出错误ht 119,"0);">if (pnp安装"到一br>也就是版退出,错误到一退出错误ht 119,"0);">if (">versio"到一,pif ;bsp;a退出,错误到一退出错误ht 119,"0);">if&nbsan>="colo退出,错误到一退出错误ht 119,"0);">if np皃an>if&nbsan>sif ;揭=屯顺 错误到一退出错误ht 119,"0);">if np皃an>if&nbsan>Ipif ;bsp;an:&if ;bsp;a="coloif ;bsp;abr>nif ;bsp;a 0)if ;bsp;anbsif ;bsp;a.2 or n:sif ;bsp;a'sif ;bsp;a退出,错误到一退出错误ht 119,"0);">if np皃an>if nppnp安装"到一br>也就是版蚷f ;bsp;a退出,错误到一退出错误ht 119,"0);">if np皃an>col"到一.sif ;bsp;a退出,错误到一退出错误ht 119,"0);">if&nbsan>if np皃an>if (pnp安装"到一.2 or newer.');退出,错误到一退出错误ht 119,"0);">if (">versio"到一(退出,错误到一退出错误ht 119,"0);">if (pnp安装"到一br>也就是版退出,错误到一退出错误ht 119,"0);">if (">versio"到一,pif ;bsp;a退出,错误到一退出错误ht 119,"0);">if&nbsan>if np皃an>if&nbsan>sif ;顺鐾退出 错误到一退出错误ht 119,"0);">if np皃an>if&nbsan>Ipif ;bsp;an:&if ;bsp;a="coloif ;bsp;abr>nif ;bbsp; 0)if ;bsp;anbsif ;bsp;a.2 or n:sif ;bsp;a'sif ;bsp;a退出,错误到一退出错误ht 119,"0);">if np皃an>if nppnp安装"到一br>也就是版蚷f ;bsp;a退出,错误到一退出错误ht 119,"0);">if&nbstyl皃an>if&nbsan>if np皃an>if nppnp安装"到一?sif ;揭煌顺,错误到一退出br到一退出br到一蚷f ;bsp;a蚷f ;bsp;a蚷f ;bsp;a.2 or newer.查看页面源码,发现如下语句:
version_compare(PHP_VERSION, '5.2', '<') and exit('Kohana requires P退出br到一.2 or newer.');
也就是版本不满足就蚷f ;顺鐾

   
strcmp退出br到一字符串比较函数,也可以进退出br到一退出br到一蚷f ;bsp;a)if ;bsp;a)if ;bsp;a,退出 错误到一退出usind到一t p分享到;">'I am using PHP 4, my&nbs瑀at瓮顺鯾r到一or: rd 退出br到一t p分享 -1e s,ra退出br到一 .2 or newer.');< 1e 正确退出br到一退出t fc到一u,lt,退出t fc到一om/pfc11 nbw-bssssssssbinp

age.js000den" n>&nse2%A9%Epanr>age.js000den" n>&nse2%A9%EUrlpanr> age.js000den" n>&nse&nf-myL ThI问齣on cobtn span; class="m错误L Th="i不秝-错误蘤cap plef,lt,p 緊&nbstag">&nf-myL ThI问齣orexpl/p>dbtn span; class="m错误Rexpl/p>d">arginw错误pfcap plef,lt,緊&nbst 119, decoration:nclass=right Sd"ztag">&npled i/bionan>&n clas PH>&n clas

'I&觯伪祍p緊&nbstag">&n clas

'I&觯伪祍p緊&nbstag">&n3 m2">w-错误蘮> fcap plef,lt, 错误pfc11 nbw-bdip dip 緊&nbstag">&nf-myL ThI问齣ore btn span; class="m错误Ctiop;转5 f错误pfc,lt,p ,lt,p ="shareitm yixin ="shareitm yixi="shareitm yi="shareitm yi緊g ztag">&nsnlgefc11 nssss荆籺 119, decoration:ncltag">&np> ulticnt"> rdc07 lass="mog Rexpl/p>dC数蕄> wzx;text-decoration:n;_zoom:1 tabitm bdwb ap ple="shareap pl荆话阌胿er> othe,帮="prigpfc11 nbw-bdi&nulticnt"> a3 m2ne-hei历史上的今天&ns="multiztabitm bsp="share,lt,p ,l="shareap pl荆籺 119, decoration:n;vi>le ty:000den;croll:0;f="http0;o.2 ne;w:000den; &nulticnt"> a3 m2ne-hei2 or读者&nulticnt"> a3 m2ne-hei热度&np> lass=yodar __rget 119, _zoom:1dimg" />