How to get the PHP Version?

Php

Php 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;

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.

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

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.
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBobView Question on Stackoverflow
Solution 1 - PhpalexView Answer on Stackoverflow
Solution 2 - PhpJohn CondeView Answer on Stackoverflow
Solution 3 - PhpAlix AxelView Answer on Stackoverflow
Solution 4 - PhpSarfrazView Answer on Stackoverflow
Solution 5 - PhpOwenView Answer on Stackoverflow
Solution 6 - PhpJordan RunningView Answer on Stackoverflow
Solution 7 - PhpNicanView Answer on Stackoverflow
Solution 8 - PhpAltaf RazzaqueView Answer on Stackoverflow
Solution 9 - PhpgabrielkView Answer on Stackoverflow
Solution 10 - PhpTijo JohnView Answer on Stackoverflow
Solution 11 - PhpHasanuzzaman SattarView Answer on Stackoverflow