To know, what are the {php} extensions loaded & version of extensions :
<?php
foreach (get_loaded_extensions() as $i => $ext)
{
echo $ext .' => '. phpversion($ext). '<br/>';
}
?>
phpversion
(PHP 4, PHP 5)
phpversion — 获取当前的PHP版本
说明
string phpversion
([ string
$extension
] )返回了包含当前运行 PHP 解释器或扩展版本信息的 string。
参数
-
extension -
可选的扩展名。
返回值
如果指定了可选参数 extension, phpversion()会返回该扩展的版本。
如果没有对应的版本信息,或者该扩展未启用,则返回 FALSE。
范例
Example #1 phpversion() 范例
<?php
// prints e.g. 'Current PHP version: 4.1.1'
echo 'Current PHP version: ' . phpversion();
// prints e.g. '2.0' or nothing if the extension isn't enabled
echo phpversion('tidy');
?>
Example #2 PHP_VERSION_ID 范例和用法
<?php
// PHP_VERSION_ID 自 PHP 5.2.7 起有效,
// 如果我们的版本低于该版本,则用以下代码来模拟
if (!defined('PHP_VERSION_ID')) {
$version = explode('.', PHP_VERSION);
define('PHP_VERSION_ID', ($version[0] * 10000 + $version[1] * 100 + $version[2]));
}
// PHP_VERSION_ID 定义为一个数字,PHP 版本越新,数字越大。
// 它的定义是以下的表达式:
//
// $version_id = $major_version * 10000 + $minor_version * 100 + $release_version;
//
// 现在我们可以通过 PHP_VERSION_ID 来检查 PHP 版本,
// 而不用每次都必须用 version_compare() 来检查 PHP 是否支持某个功能。
//
// 比如,我们在此可以定义一系列 PHP_VERSION_* constants 常量,
// 而在 5.2.7 之前的版本并没有被定义。
if (PHP_VERSION_ID < 50207) {
define('PHP_MAJOR_VERSION', $version[0]);
define('PHP_MINOR_VERSION', $version[1]);
define('PHP_RELEASE_VERSION', $version[2]);
// 等等, ...
}
?>
注释
Note:
这些信息也存在于预定义常量
PHP_VERSION里。 更多版本的信息可以使用常量PHP_VERSION_*。
参见
- 常量 PHP_VERSION
- version_compare() - 对比两个「PHP 规范化」的版本数字字符串
- phpinfo() - 输出关于 PHP 配置的信息
- phpcredits() - 打印 PHP 贡献者名单
- php_logo_guid() - 获取 logo 的 guid
- zend_version() - 获取当前 Zend 引擎的版本
pavankumar at tutorvista dot com ¶
2 years ago
bitworks at web dot de ¶
3 years ago
to realize if the actual version ist newer than '5.2.10'
simply use:
<?php
if (strnatcmp(phpversion(),'5.2.10') >= 0)
{
# equal or newer
}
else
{
# not sufficiant
}
?>
cHao ¶
13 days ago
If you're trying to check whether the version of PHP you're running on is sufficient, don't screw around with `strcasecmp` etc. PHP already has a `version_compare` function, and it's specifically made to compare PHP-style version strings.
<?php
if (version_compare(phpversion(), '5.3.10', '<')) {
// php version isn't high enough
}
?>
Sam Yong - hellclanner at live dot com ¶
3 years ago
Take note that if you pass phpversion() with an empty string, eg.
<?php
echo phpversion('');
?>
It will not return anything, since it cannot find an extension with the name string(0) ''.
dmitry DOT seredinov AT gmail DOT com ¶
4 years ago
To simple receive a major PHP version value (e.g., 5.2), you can use next:
<?php
// Output below will looks like '5.2', depends to your version
echo floatval(phpversion());
?>
pl DOT baasch AT skycube DOT net ¶
4 years ago
In a addition to phpversion,..
if you've got a system like ubuntu or some else, you get
<?php
echo phpversion(); // 5.2.4-2ubuntu5.2
?>
To fix this, use the following:
<?php
echo substr(phpversion(),0,strpos(phpversion(), '-'));
?>
