How to get the PHP Version?
PhpPhp Problem Overview
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
$version = way_to_get_version();
print $version;
would print 5.3.0 on one machine, and 5.3.1 on another machine.
Php Solutions
Solution 1 - Php
$version = phpversion();
print $version;
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.
Solution 2 - Php
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.
Solution 3 - Php
You can either use the phpversion()
function or the PHP_VERSION
constant.
To compare versions you should always rely on version_compare()
.
Solution 4 - Php
.........
if (version_compare(phpversion(), '5', '>='))
{
// act accordintly
}
Solution 5 - Php
Take a look at http://php.net/phpversion">phpversion()</a>;.
echo "Current version is PHP " . phpversion();
Solution 6 - Php
phpversion()
will tell you the currently running PHP version.
Solution 7 - Php
http://us.php.net/manual/en/function.phpversion.php
Returns exactly the "5.3.0".
Solution 8 - Php
you can use phpversion() function to get php version
eg. echo 'PHP version: ' . phpversion();
Solution 9 - Php
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()
][1]
[1]: http://php.net/manual/en/function.function-exists.php "function_exists"
Solution 10 - Php
You can use phpversion(); function to find the current version
<?php echo 'Current PHP version: ' . phpversion(); ?>
Solution 11 - Php
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.
}