Reputation: 1192
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.
Upvotes: 82
Views: 153879
Reputation: 219914
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 5
echo $PHP_VERSION;
That does not work in 7+ however, you can use:
php7.x
echo PHP_VERSION;
Not sure about php8.x though. constants are always faster then function calls.
Upvotes: 34
Reputation: 39
Surprised this answer hasn't been added (here's how you would actually run this from a command line):
php -r "echo 'PHP version: ' . phpversion();"
Upvotes: 2
Reputation: 1523
You actually do not need any of the following
phpversion()
PHP_VERSION
version_compare()
To properly compare the current version.
You can use the PHP_VERSION_ID
constant. This is for 7.2 and its NOT 720 think 70.20.0
if ( PHP_VERSION_ID < 70200 ) {
echo "php version is lower than 7.2";
}
Upvotes: 1
Reputation: 600
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.
}
Upvotes: -1
Reputation: 490597
$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.
Upvotes: 122
Reputation: 382881
.........
if (version_compare(phpversion(), '5', '>='))
{
// act accordintly
}
Upvotes: 13
Reputation: 563
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()
Upvotes: 1
Reputation: 154673
You can either use the phpversion()
function or the PHP_VERSION
constant.
To compare versions you should always rely on version_compare()
.
Upvotes: 20
Reputation: 106097
phpversion()
will tell you the currently running PHP version.
Upvotes: 4
Reputation: 7945
http://us.php.net/manual/en/function.phpversion.php
Returns exactly the "5.3.0".
Upvotes: 4
Reputation: 84573
Take a look at phpversion().
echo "Current version is PHP " . phpversion();
Upvotes: 10