php版本比较函数version_compare()

巴扎黑
发布: 2017-03-13 15:51:46
原创
4386人浏览过

php中比较php版本,一般用version_compare()函数。
用途: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 

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

If you specify the third optional operator argument, you can test for a particular relationship. The possible operators are: , gt, >=, ge, ==, =, eq, !=, , ne respectively.

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

立即学习PHP免费学习笔记(深入)”;

    返回值:

By default, version_compare() returns -1 if the first version is lower than the second, 0 if they are equal, and1 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', '也就是版本不满足就退出。

    strcmp()的字符串比较函数,也可以进行比较,但像下面这样,就会出现错误:
4.1.10与4.1.2比较
strcmp(“4.1.10″, “4.1.2″); 会返回-1 错误
version_compare(“4.1.10″. “4.1.2″); 就会返回1 正确

PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号