Skip to content

Instantly share code, notes, and snippets.

@SpekkoRice
Last active July 21, 2020 07:40
Show Gist options
  • Save SpekkoRice/694e4e33ee298361b642 to your computer and use it in GitHub Desktop.
Save SpekkoRice/694e4e33ee298361b642 to your computer and use it in GitHub Desktop.
Bash: Check the version of PHP
#!/bin/bash
# Function to check if the PHP version is valid
checkPHPVersion() {
# The current PHP Version of the machine
PHPVersion=$(php -v|grep --only-matching --perl-regexp "5\.\\d+\.\\d+");
# Truncate the string abit so we can do a binary comparison
currentVersion=${PHPVersion::0-2};
# The version to validate against
minimumRequiredVersion=$1;
# If the version match
if [ $(echo " $currentVersion >= $minimumRequiredVersion" | bc) -eq 1 ]; then
# Notify that the versions are matching
echo "PHP Version is valid ...";
else
# Else notify that the version are not matching
echo "PHP Version NOT valid for ${currentVersion} ...";
# Kill the script
exit 1
fi
}
@dcondrey
Copy link

dcondrey commented Dec 9, 2016

php --version | tail -r | tail -n 1 | cut -d " " -f 2 | cut -c 1,3
# for php71 output is 71

useful for such things as

phpversion="$(php --version | tail -r | tail -n 1 | cut -d " " -f 2 | cut -c 1,3)"
brew unlink php$phpversion

@falvarez
Copy link

For systems lacking of tail -r, this should also work:

php --version | head -n 1 | cut -d " " -f 2 | cut -c 1,3

@ozh
Copy link

ozh commented Oct 24, 2017

php -r "echo PHP_VERSION;" ???? Simple ??

@dkimery256
Copy link

Thanks for this! Tweaked it some to meet my needs. I don't know much about shell scripts, but I am getting there.

@larssn
Copy link

larssn commented Jun 20, 2019

export PHP_VERSION=$(php -r "echo PHP_VERSION;" | grep --only-matching --perl-regexp "7.\d+")

@rrsai
Copy link

rrsai commented Oct 14, 2019

Core script is not compatible with php versions greater than 5, apparently.

@calebjcox
Copy link

It doesn't work for anything but PHP 5 because it's hardcoded to look for versions starting with 5. However, switching the 5 out for a generic digit matches operating system information so you have to narrow the search to things preceeded by "PHP ". Then you have to trim them PHP part out of the result.

# The current PHP Version of the machine
PHPVersion=$(php -v|grep --only-matching --perl-regexp "(PHP )\d+\.\\d+\.\\d+");
echo $PHPVersion
# Truncate the string abit so we can do a binary comparison
currentVersion=${PHPVersion:4:3};

@haljeshi
Copy link

It doesn't work for anything but PHP 5 because it's hardcoded to look for versions starting with 5. However, switching the 5 out for a generic digit matches operating system information so you have to narrow the search to things preceeded by "PHP ". Then you have to trim them PHP part out of the result.

# The current PHP Version of the machine
PHPVersion=$(php -v|grep --only-matching --perl-regexp "(PHP )\d+\.\\d+\.\\d+");
echo $PHPVersion
# Truncate the string abit so we can do a binary comparison
currentVersion=${PHPVersion:4:3};

Thank you for the tip. I have modified it to use cut so that I can use it in a shell script. The final result is:
php -v|grep --only-matching --perl-regexp "(PHP )\d+\.\\d+\.\\d+"|cut -c 5-7

And I used it to create the following shell script:
https://gist.github.com/haljeshi/329a0fb0c6df43f3fc904d716a96af1b

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment