Skip to content

Instantly share code, notes, and snippets.

@elipettingale
Last active July 21, 2022 08:13
Show Gist options
  • Select an option

  • Save elipettingale/0206909cca9243e9be3d71ab19d8e1e1 to your computer and use it in GitHub Desktop.

Select an option

Save elipettingale/0206909cca9243e9be3d71ab19d8e1e1 to your computer and use it in GitHub Desktop.
PHP Version Manager - quickly switch terminal to a specific version of php
function pvm() {
if [ $1 = "use" ]
then
if [ -z "$2" ]
then
if [ -f ".pvmrc" ]
then
VERSION=$(cat .pvmrc)
eval "pvm use $VERSION"
else
printf "\033[1;31mNo .pvmrc file found\033[0m\n"
fi
elif [ $2 = "latest" ]
then
LATEST=$(find /usr/bin -regextype sed -regex "/usr/bin/php[0-9].[0-9]" | sort -n | tail -n 1)
LATEST=${LATEST:12}
eval "pvm use $LATEST"
else
if [ -f "/usr/bin/php${2}" ]
then
printf "Now using php $2\n"
sudo update-alternatives --set php /usr/bin/php$2
sudo update-alternatives --set php-config /usr/bin/php-config$2
sudo update-alternatives --set phpize /usr/bin/phpize$2
else
printf "\033[1;31mVersion not found at /usr/bin/php${2}\033[0m\n"
fi
fi
fi
if [ $1 = "list" ]
then
VERSIONS=$(find /usr/bin -regextype sed -regex "/usr/bin/php[0-9].[0-9]" | sort -n)
VERSIONS=($VERSIONS)
CURRENT_LONG=$(php -v | head -n 1 | cut -d " " -f 2)
CURRENT=$(echo $CURRENT_LONG | cut -c 1-3)
for i in "${VERSIONS[@]}"
do
if [ ${i:12} = $CURRENT ]
then
printf "\033[1;32m-> ${CURRENT} (${CURRENT_LONG})\033[0m\n"
else
echo "${i:12}"
fi
done
fi
if [ $1 = "-v" ]
then
CURRENT=$(php -v | head -n 1 | cut -d " " -f 2 | cut -c 1-3)
echo $CURRENT
fi
}
@elipettingale
Copy link
Author

FYI, the regex is lazy: won't work when php gets to version 10.

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