How to get the PHP Version?
Is there a way to check the version of PHP that executed a particular script from within that script? So for example, 开发者_JAVA技巧the following snippet
$version = way_to_get_version();
print $version;
would print 5.3.0 on one machine, and 5.3.1 on another machine.
$version = phpversion();
print $version;
Documentation
However, for best practice, I would use the constant PHP_VERSION
. No function overhead, and cleaner IMO.
Also, be sure to use version_compare()
if you are comparing PHP versions for compatibility.
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.
echo PHP_VERSION;
constants are always faster then function calls.
You can either use the phpversion()
function or the PHP_VERSION
constant.
To compare versions you should always rely on version_compare()
.
.........
if (version_compare(phpversion(), '5', '>='))
{
// act accordintly
}
Take a look at phpversion().
echo "Current version is PHP " . phpversion();
http://us.php.net/manual/en/function.phpversion.php
Returns exactly the "5.3.0".
phpversion()
will tell you the currently running PHP version.
you can use phpversion() function to get php version
eg. echo 'PHP version: ' . phpversion();
You can use phpversion(); function to find the current version
<?php echo 'Current PHP version: ' . phpversion(); ?>
phpversion()
is one way. As John conde said, PHP_VERSION
is another (that I didn't know about 'till now).
You may also be interested in function_exists()
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.
$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.
}
精彩评论