There are multiple fancy scripts out there for managing PHP versions, but here I will show you how you can get the same result with just Homebrew, which you probably already have installed.
Install different PHP versions:
brew install php@7.2
brew install php@7.3
brew install php@7.4
brew install php@8.0
Let’s say you want version 8.0
, simply run:
brew unlink php && brew link php@8.0
Result:
PHP 8.0.2 (cli) (built: Feb 4 2021 17:58:53) ( NTS )
Copyright (c) The PHP Group
Zend Engine v4.0.2, Copyright (c) Zend Technologies
with Zend OPcache v8.0.2, Copyright (c), by Zend Technologies
Simply repeat the unlink/link command every time you want to switch version.
These commands have been tested on OSX Catalina but should also work for earlier versions of OSX.