Skip to content

Instantly share code, notes, and snippets.

@christianhanne
Created August 10, 2017 15:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save christianhanne/dbed3f9848908774ad2eb4cc34f5b2fe to your computer and use it in GitHub Desktop.
Save christianhanne/dbed3f9848908774ad2eb4cc34f5b2fe to your computer and use it in GitHub Desktop.
Change PHP version in terminal using MAMP.
#!/bin/bash
PHP_DIR="/Applications/MAMP/bin/php/"
BIN_DIR="/usr/local/bin/"
function use_version {
PHP_VERSION_PATH="${PHP_DIR}php${1}/bin/"
if [ ! -d "$PHP_VERSION_PATH" ]; then
echo "PHP version $1 does not exist."
exit 1
fi
# Ask for permissions right away
sudo -v
# Symlink all relevant binaries.
sudo rm -f "${BIN_DIR}php" "${BIN_DIR}pear" "${BIN_DIR}pecl"
sudo ln -s "${PHP_VERSION_PATH}php" "${BIN_DIR}php"
sudo ln -s "${PHP_VERSION_PATH}pear" "${BIN_DIR}pear"
sudo ln -s "${PHP_VERSION_PATH}pecl" "${BIN_DIR}pecl"
# Output the now active php version.
echo "Now using the following php version:"
. ~/.bash_profile
php -v
exit
}
function list_versions {
for f in $(ls $PHP_DIR | grep php)
do
echo ${f#"php"}
done
}
case "$1" in
use)
use_version $2
;;
list)
list_versions
;;
*)
echo "Usage: $0 {list|use version}"
esac
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment