Skip to content

Instantly share code, notes, and snippets.

@othyn
Last active December 4, 2021 13:11
Show Gist options
  • Save othyn/5f92f92f904ed59860012a92a29af26e to your computer and use it in GitHub Desktop.
Save othyn/5f92f92f904ed59860012a92a29af26e to your computer and use it in GitHub Desktop.
Easy ZSH/Bash alias for swapping PHP versions on the fly
# Switch PHP version in brew
# Place this in your ~/.bashrc or similar for autoload into your shell
function phpsw() {
if [ -z "$1" ]; then
logInfo "You need to provide a PHP formulae version to switch to in format:"
logInfo "$ phpsw 8.1"
if command -v jq >/dev/null 2>&1; then
logInfo "Installed PHP versions via Brew:"
brew info --json php | jq -r '.[].versioned_formulae[]'
else
logInfo "Please install jq to list installed PHP versions via: $ brew install jq"
fi
return 1
fi
local phpFormulaeVersion="php@$1"
if brew ls --versions $phpFormulaeVersion > /dev/null; then
logInfo "${phpFormulaeVersion} is already installed, continuing..."
else
logInfo "${phpFormulaeVersion} is not installed, installing..."
brew install $phpFormulaeVersion
logInfo "${phpFormulaeVersion} installed!"
fi
logInfo "Now linking ${phpFormulaeVersion} and making it active!"
brew unlink php && brew link --force --overwrite $phpFormulaeVersion
logInfo "Done! ${phpFormulaeVersion} is now active:"
php -v
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment