Skip to content

Instantly share code, notes, and snippets.

@othyn
Last active Dec 4, 2021
Embed
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[]'
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