How To Check The Php Version

Is there a way to kiểm tra 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, và 5.3.1 on another machine.

Bạn đang xem: How to check the php version



$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 vì chưng 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.

Xem thêm: tên đẹp cho bé gái

To compare versions you should always rely on version_compare().

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 đầu ra of phpversion() lớn a floating point number, it will give you the major và minor version parts. This way you can implement PHP compatibility easily.

$version = (float)phpversion();if ($version > 7.0) //do something for php7.1 và above. Elseif ($version === 7.0) //do something for php7.0 else //do something for php5.6 or lower.
Thanks for contributing an answer to lớn Stack Overflow!

Please be sure to lớn answer the question. Provide details và share your research!

But avoid

Asking for help, clarification, or responding to other answers.Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.

Post Your Answer Discard

By clicking “Post Your Answer”, you agree to lớn our terms of service, privacy policy and cookie policy

site thiết kế / hình ảnh © 2022 Stack Exchange Inc; user contributions licensed under cc by-sa. Rev2022.2.11.41438

Your privacy

By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.