Skip to content

Instantly share code, notes, and snippets.

@mrunkel
Created June 13, 2021 22:51
Show Gist options
  • Save mrunkel/e66b7c3e927dedfd0d45188812a6ba8f to your computer and use it in GitHub Desktop.
Save mrunkel/e66b7c3e927dedfd0d45188812a6ba8f to your computer and use it in GitHub Desktop.
script snippet that generates aliases for each version of PHP installed by brew.

switchphp

installation

Install in your .bashrc or .zshrc file to create an alias for each version of php installed via brew.

Ie, if you have PHP v7.4 and v8.0 installed, it will create two aliases 7.4, and 8.0 which will switch to that version of PHP.

# switch PHP versions
installedPhpVersions=($(brew ls --version | egrep -o 'php(:?@\d.\d)?\s(\d\.\d)' | cut -f2 -d' '))
for phpVersion in ${installedPhpVersions[*]}; do
value="{"
for otherPhpVersion in ${installedPhpVersions[*]}; do
if [ "${otherPhpVersion}" != "${phpVersion}" ]; then
value="${value} brew unlink php@${otherPhpVersion};"
fi
done
value="${value} brew link php@${phpVersion} --force --overwrite; } &> /dev/null && php -v"
alias "${phpVersion}"="${value}"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment