Skip to content

Instantly share code, notes, and snippets.

Last active December 4, 2021 13:11
What would you like to do?
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[]'
logInfo "Please install jq to list installed PHP versions via: $ brew install jq"
return 1
local phpFormulaeVersion="php@$1"
if brew ls --versions $phpFormulaeVersion > /dev/null; then
logInfo "${phpFormulaeVersion} is already installed, continuing..."
logInfo "${phpFormulaeVersion} is not installed, installing..."
brew install $phpFormulaeVersion
logInfo "${phpFormulaeVersion} installed!"
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