4 Ways to Check The PHP Version (A Quick Guide)

Welcome to a quick tutorial on how to check the PHP version. Yep, this is probably one of the “funky” tutorials that I have written. The PHP version is normally one that people will not touch on, but be it for checking for compatibility or if it is time for an update.

There are 4 possible ways to do a version check on your current PHP installation:

  1. Run php -v in the command line.
  2. If you need in-depth information on your installation, create a single-line script – phpinfo();
  3. Or use the version constant – echo PHP_VERSION;
  4. Finally, the PHP version function – echo phpversion();

That should cover all the basics, but read on if you need more details.

 

 

TABLE OF CONTENTS

 

DOWNLOAD & NOTES

Here is the download link to the example code, so you don’t have to copy-paste everything.

 

EXAMPLE CODE DOWNLOAD

Source code on GitHub Gist

Just click on “download zip” or do a git clone. I have released it under the MIT license, so feel free to build on top of it or use it in your own project.

 

SORRY FOR THE ADS...

But someone has to pay the bills, and sponsors are paying for it. I insist on not turning Code Boxx into a "paid scripts" business, and I don't "block people with Adblock". Every little bit of support helps.

Buy Me A Coffee Code Boxx eBooks

 

PHP VERSION CHECK

All right, let us now get into the various to check the PHP version in this section.

 

TUTORIAL VIDEO

 

1) COMMAND LINE PHP VERSION CHECK

1-command.txt
D:\>php -v
PHP 8.2.4 (cli) (built: Mar 14 2023 17:54:25) (ZTS Visual C++ 2019 x64)
Copyright (c) The PHP Group
Zend Engine v4.2.4, Copyright (c) Zend Technologies

Windows users, launch the command prompt and fire up php -v. Mac and Linux users, that is pretty much the same, except we call it terminal instead of command prompt.

 

 

2) PHP INFO

2-phpinfo.php
<?php
phpinfo();

This is another useful function phpinfo() – Just create a one-line script with it, and access it from the browser. That’s it, the PHP version is nice, big, and bold right at the top. You can also get all the information on the enabled extensions here.

 

 

3) PHP VERSION CONSTANTS

3-ver-const.php
<?php
// FULL PHP VERSION STRING IN FORMAT OF -
// MAJOR.MINOR.RELEASE[EXTRA]
echo "Full - " . PHP_VERSION . "<br>";
echo "Major - " . PHP_MAJOR_VERSION . "<br>";
echo "Minor - " . PHP_MINOR_VERSION . "<br>";
echo "Release - " . PHP_RELEASE_VERSION . "<br>";
echo "Extra - " . PHP_EXTRA_VERSION . "<br>";
echo "Version ID - " . PHP_VERSION_ID . "<br>";

There are also 6 different constants that are related to the PHP version… If you really want to dig that deep.

 

4) PHP VERSION FUNCTION

4-php-version.php
<?php
// PHP VERSION
echo "Current PHP version is " . phpversion() . "<br>";
 
// EXTENSION VERSIONS
$extensions = get_loaded_extensions();
foreach ($extensions as $k=>$ext) {
  echo $ext . " - " . phpversion($ext) . "<br>";
}

PHP comes with a very handy function called phpversion(). We can use it to get the PHP version, and also the version of the extensions.

 

 

5) HOW TO CHECK FOR 32 OR 64 BITS PHP

5-bits.php
<?php
if (PHP_INT_SIZE==4) {
  echo "32 bits";
}
if (PHP_INT_SIZE==8) {
  echo "64 bits";
}

To check if you are running on 64-bit, just look under “Architecture” in phpinfo() – X86 is 32-bit, and X64 is 64-bit. Otherwise, if you want to test it out programmatically, you can check the PHP_INT_SIZE constant. It should be 4 for 32-bit, and 8 for 64-bit. Credits to the original post on StackOverflow.

 

6) HOW TO CHECK FOR THREAD-SAFE PHP

6-thread-safe.php
<?php
echo ZEND_THREAD_SAFE ? "Thread safe" : "Not thread safe";

Run phpinfo() and look for “Thread Safety”. Similarly, under the PHP extension build, there will be a TS if it is the thread-safe version. Programmatically wise, we can check with the PHP_INT_SIZE constant.

 

 

EXTRAS

That’s all for this tutorial, and here is a small section on some extra links that may be useful to you.

 

SEMANTIC VERSIONING

So, just what the heck is with the X.Y.Z way of versioning? That is called “semantic versioning”, and there is a good reason behind it.

  • X is the major version. This number goes up only when there are incompatible or irreversible changes.
  • Y is the minor version. When feature changes are made, but still backward compatible.
  • Z refers to the patch version. Something like “nothing has changed, just a bug fix”.

 

LINKS & REFERENCES

 

THE END

Thank you for reading, and we have come to the end of this short guide. I hope that it has helped you to better understand, and if you want to share anything with this guide, please feel free to comment below. Good luck and happy coding!