PHP: How to check PHP version?
Use this native function: phpversion(). In my case it returns ...
php > echo phpversion();
7.4.3
If you just need to check if php version is 7 or higher ...
if ((int) phpversion() < 7) {
throw new \Error("Sorry, PHP >=7 required");
}
Get the PHP version number only
To get full version (5.4.6.1)
<?php
preg_match("#^\d+(\.\d+)*#", PHP_VERSION, $match);
echo $match[0];
It will return 5.4.6.1 in your example
To get version as you need (5.4)
preg_match("#^\d.\d#", "5.4.6.1ububtu1.8", $match);
echo $match[0];
It will return 5.4 in your example
Test here
Bash script to get php version
If you're building your automations with apt, then you should be relying on apt and/or dpkg to get the relevant information.
dpkg -l 'php*' | grep ^ii
For completenes, yum/dnf/rpm:
rpm -qa 'php*'
Related Topics
Pdostatement (Mysql): Inserting Value 0 into a Bit(1) Field Results in 1 Written in Table
Adding Custom Field to Product Category
Can Someone Explain the /E Regex Modifier
Get the Keys for Duplicate Values in an Array
How to Make Strpos Case Insensitive
Will Enabling Xdebug on a Production Server Make PHP Slower
Simplest Way to Detect Client Locale in PHP
How to Kill A/All PHP Sessions
Binding Params for Pdo Statement Inside a Loop
Why Is Crontab Not Executing My PHP Script
How to Set a Cookie and Then Redirect in PHP
How to Solve General Error: 2006 MySQL Server Has Gone Away
PHP Getting Unique Values of a Multidimensional Array
Is It Ever Ok to Store Password in Plain Text in a PHP Variable or PHP Constant
How to Check If an Uploaded File Is an Image Without Mime Type