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.

ⓘ I have included a zip file with all the example source code at the start of this tutorial, so you don’t have to copy-paste everything… Or if you just want to dive straight in.

 

 

QUICK SLIDES

 

TABLE OF CONTENTS

Download & Notes Version Check Useful Bits & Links
Tutorial Video The End

 

DOWNLOAD & NOTES

First, here is the download link to the example code as promised.

 

QUICK NOTES

If you spot a bug, feel free to comment below. I try to answer short questions too, but it is one person versus the entire world… If you need answers urgently, please check out my list of websites to get help with programming.

 

EXAMPLE CODE DOWNLOAD

Click here to download the example source code, I have released it under the MIT license, so feel free to build on top of it or use it in your own project.

 

 

PHP VERSION CHECK

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

 

1) COMMAND LINE PHP VERSION CHECK

1-command.txt
D:\>php -v
PHP 8.0.8 (cli) (built: Jun 29 2021 16:02:52) ( ZTS Visual C++ 2019 x64 )
Copyright (c) The PHP Group
Zend Engine v4.0.8, 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, 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.

 

 

USEFUL BITS & LINKS

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

 

TUTORIAL VIDEO

 

INFOGRAPHIC CHEATSHEET

Ways to Check PHP Version (click to enlarge)

 

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!

Leave a Comment

Your email address will not be published. Required fields are marked *