Skip to content

Instantly share code, notes, and snippets.

@imjlk
Created October 21, 2022 10:58
Show Gist options
  • Save imjlk/531cb75c8be411e78c09b128bd453575 to your computer and use it in GitHub Desktop.
Save imjlk/531cb75c8be411e78c09b128bd453575 to your computer and use it in GitHub Desktop.
Add aliases of switch PHP versions on Mac OS
# 1. Need to install PHP with `brew install ...`
# 2. Need to install grep `brew install grep`
# 3. Paste it in your `~/.zshrc`
# Updated from https://localheinz.com/blog/2020/05/05/switching-between-php-versions-when-using-homebrew/
# determine versions of PHP installed with HomeBrew
installedPhpVersions=($(brew ls --versions | ggrep -E 'php(@.*)?\s' | ggrep -oP '(?<=\s)\d\.\d' | uniq | sort))
# create alias for every version of PHP installed with HomeBrew
for phpVersion in ${installedPhpVersions[*]}; do
value="{"
for otherPhpVersion in ${installedPhpVersions[*]}; do
if [ "${otherPhpVersion}" = "${phpVersion}" ]; then
continue
fi
# unlink other PHP version
value="${value} brew unlink php@${otherPhpVersion};"
done
# link desired PHP version
value="${value} brew link php@${phpVersion} --force --overwrite; } &> /dev/null && echo '${phpVersion}' > ~/.php_version && source ~/.zshrc && php -v"
alias "${phpVersion}"="${value}"
done
export PHP_VERSION=$(<~/.php_version)
export PATH="/opt/homebrew/opt/php@$PHP_VERSION/bin:$PATH"
export PATH="/opt/homebrew/opt/php@$PHP_VERSION/sbin:$PATH"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment