INTRODUCTION
THE LESS KNOWN FEATURE
Welcome to a Tutorial on how to check the PHP version. Yep, this is probably one of the weirder tutorials that I have ever written. The PHP version is normally one that people will not touch one. But if you want to do a version check on your application for compatibility, or to check if it is time for an upgrade – This guide will walk you through ways to get the PHP version. Read on to find out.
I have included a zip file with all the example source code at the end of this tutorial, so you don’t have to copy-paste everything… Or if you just want to dive straight in.
CONFESSION
AN HONEST DISCLOSURE
Quick, hide your wallets! I am an affiliate partner of Google, eBay, Adobe, Bluehost, Clickbank, and more. There are affiliate links and advertisements throughout this website. Whenever you buy things from the evil links that I recommend, I will make a commission. Nah. These are just things to keep the blog going, and allows me to give more good stuff to you guys - for free. So thank you if you decide to pick up my recommendations!
NAVIGATION
TABLE OF CONTENTS
Section A | Section B | Extra |
Extra | Closing |
SECTION A
WAYS TO GET THE VERSION
There are actually quite a number of different ways to the PHP version on your machine – We will walk through all of it in this section.
OPTION 1) Command LINE
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.
D:\>php -v
PHP 7.2.5 (cli) (built: Apr 25 2018 02:39:21) ( ZTS MSVC15 (Visual C++ 2017) x86 )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
OPTION 2) SCRIPT FUNCTION
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.
";
// Extensions
$extensions = get_loaded_extensions();
foreach ($extensions as $k=>$ext) {
echo $ext . " - " . phpversion($ext) . "
";
}
?>
OPTION 3) SCRIPT CONSTANT
There are also 6 different constants that are related to the PHP version… If you really want to dig that deep.
";
echo "Major - " . PHP_MAJOR_VERSION . "
";
echo "Minor - " . PHP_MINOR_VERSION . "
";
echo "Release - " . PHP_RELEASE_VERSION . "
";
echo "Extra - " . PHP_EXTRA_VERSION . "
";
echo "Version ID - " . PHP_VERSION_ID . "
";
?>
OPTION 4) PHP INFO
This is another useful function phpinfo()
– Just create a one-line-script with it, and access it from the browser.
There it is, the PHP version is nice and big right at the top. You can also get all the information on the enabled extensions here.
THE CHEAT SHEET
SECTION B
MORE VERSION INFO
And so there are 4 different ways to get the PHP version, but there’s more – What if we need to know it is a 32-bit or 64-bit version?
32 OR 64 BITS
To check if you are running on 64-bit, just look under “Architecture” in the PHP info – 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.
THREAD SAFE OR NOT
Similarly, look under PHP info – Search for “Thread Safety”. Programmatically wise, we can simply check the PHP_INT_SIZE
constant.
EXTRA
USEFUL BITS
That’s all for this project, and here is a small section on some extra links that may be useful to you.
REFERENCES
- Official PHP manual – version function.
- Reserved constants in PHP.
EXTRA
DOWNLOAD
Finally, here is the download link to the example code as promised.
SOURCE 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.
CLOSING
WHAT’S NEXT?
Thank you for reading, and we have come to the end of this guide. I hope that it has helped you in your project, and if you want to share anything with this guide, please feel free to comment below. Good luck and happy coding!
The post 4 Ways to Check PHP Version appeared first on Code Boxx.