php 如何获取PHP版本?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2113955/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get the PHP Version?
提问by Bob
Is there a way to check the version of PHP that executed a particular script from within that script? So for example, the following snippet
有没有办法检查从该脚本中执行特定脚本的 PHP 版本?例如,以下代码段
$version = way_to_get_version();
print $version;
would print 5.3.0 on one machine, and 5.3.1 on another machine.
将在一台机器上打印 5.3.0,在另一台机器上打印 5.3.1。
回答by alex
$version = phpversion();
print $version;
However, for best practice, I would use the constant PHP_VERSION. No function overhead, and cleaner IMO.
但是,为了最佳实践,我会使用常量PHP_VERSION. 没有功能开销,更干净的 IMO。
Also, be sure to use version_compare()if you are comparing PHP versions for compatibility.
此外,version_compare()如果您要比较 PHP 版本的兼容性,请务必使用。
回答by John Conde
Technically the best way to do it is with the constant PHP_VERSION as it requires no function call and the overhead that comes with it.
从技术上讲,最好的方法是使用常量 PHP_VERSION,因为它不需要函数调用和随之而来的开销。
echo PHP_VERSION;
constants are always faster then function calls.
常量总是比函数调用快。
回答by Alix Axel
You can either use the phpversion()function or the PHP_VERSIONconstant.
您可以使用phpversion()函数或PHP_VERSION常量。
To compare versions you should always rely on version_compare().
要比较版本,您应该始终依赖version_compare().
回答by Owen
回答by Sarfraz
.........
…………
if (version_compare(phpversion(), '5', '>='))
{
// act accordintly
}
回答by Jordan Running
phpversion()will tell you the currently running PHP version.
phpversion()会告诉你当前运行的 PHP 版本。
回答by Nican
http://us.php.net/manual/en/function.phpversion.php
http://us.php.net/manual/en/function.phpversion.php
Returns exactly the "5.3.0".
准确返回“5.3.0”。
回答by gabrielk
phpversion()is one way. As John conde said, PHP_VERSIONis another (that I didn't know about 'till now).
phpversion()是一种方式。正如约翰康德所说,PHP_VERSION是另一个(直到现在我还不知道)。
You may also be interested in function_exists()
您也可能对。。。有兴趣 function_exists()
回答by Hasanuzzaman Sattar
If you typecast the output of phpversion() to a floating point number, it will give you the major and minor version parts. This way you can implement PHP compatibility easily.
如果您将 phpversion() 的输出类型转换为浮点数,它将为您提供主要和次要版本部分。通过这种方式,您可以轻松实现 PHP 兼容性。
$version = (float)phpversion();
if ($version > 7.0) {
//do something for php7.1 and above.
} elseif ($version === 7.0) {
//do something for php7.0
} else {
//do something for php5.6 or lower.
}

