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 |