Skip to content

Instantly share code, notes, and snippets.

@Chemaclass
Last active March 17, 2024 12:26
Show Gist options
  • Save Chemaclass/64ea700ab61cff24cc22dae9f37193b6 to your computer and use it in GitHub Desktop.
Save Chemaclass/64ea700ab61cff24cc22dae9f37193b6 to your computer and use it in GitHub Desktop.
Managing diff local PHP versions in MacOS

Managing diff local PHP versions in MacOS

Identify which php versions do you have installed locally

  • brew search php

Install the versions that you need

Enable your desired version

Verify the current PHP version

  • php -v

How to switch between the different versions?

Now that you have all versions installed, you can switch between them effortlessly. Add this function to your .zshrc:

# Switch PHP version
function sphp() {
    Yellow='\033[0;33m'
    Color_Off='\033[0m'
    
    brew unlink php@$1 && brew link --overwrite --force php@$1

    echo "${Yellow}Validating current PHP Version${Color_Off}"
    php -v
}

Usage example

  • sphp 7.4
  • sphp 8.0
  • sphp 8.1
  • sphp 8.2
  • sphp 8.3

Demo

Screenshot 2024-03-17 at 13 19 05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment