Switching PHP version locally with Brew for macOS

Published on 2021-01-01 • Modified on 2021-01-01

In this snippet, we will see how to switch PHP version locally when using Brew for MacOS. I didn't use this a lot in the past because it was OK to develop with PHP 7.4 when working with a project locked to 7.2 using the PHPStorm "PHP Language level" option. But with the arrival of PHP 8, it's not possible anymore. With these simple scripts, you can change your current PHP version in less than a second.


## —— PHP 🐘 (macOS with brew) —————————————————————————————————————————————————
php-upgrade: ## Upgrade PHP to the last version
	@$(BREW) upgrade php

php-set-8-2: ## Set php 8.2 as the current PHP version
	@$(BREW) unlink php
	@$(BREW) link --overwrite php@8.2

php-set-8-3: ## Set php 8.3 as the current PHP version
	@$(BREW) unlink php
	@$(BREW) link --overwrite php@8.3

php-set-8-1: ## Set php 8.1 as the current PHP version
	@$(BREW) unlink php
	@$(BREW) link --overwrite php@8.1

#[12:36:23] coil@mac-mini.home:/Users/coil/Sites/strangebuzz.com$ php -v
#PHP 7.4.13 (cli) (built: Nov 30 2020 14:33:39) ( NTS )
#Copyright (c) The PHP Group
#Zend Engine v3.4.0, Copyright (c) Zend Technologies
#with Zend OPcache v7.4.13, Copyright (c), by Zend Technologies

#[12:36:32] coil@mac-mini.home:/Users/coil/Sites/strangebuzz.com$ make php-set-8-0
#brew unlink php
#Unlinking /usr/local/Cellar/php/8.0.0_1... 0 symlinks removed
#brew link --overwrite php@8.0
#Unlinking /usr/local/Cellar/php@7.4/7.4.13_1... 25 symlinks removed
#Linking /usr/local/Cellar/php/8.0.0_1... 24 symlinks created

#[12:36:35] coil@mac-mini.home:/Users/coil/Sites/strangebuzz.com$ php -v
#PHP 8.0.0 (cli) (built: Nov 30 2020 13:43:08) ( NTS )
#Copyright (c) The PHP Group
#Zend Engine v4.0.0-dev, Copyright (c) Zend Technologies
#with Zend OPcache v8.0.0, Copyright (c), by Zend Technologies

 More on Stackoverflow   Read the doc  Random snippet

  Work with me!