Blog
Published on

How to switch PHP versions with Laravel Valet

Authors

Recently I found myself working on a legacy project that runs on PHP 7.2. It mostly works as is with PHP 7.4 but some library does not support some of the changes from PHP 7.4, in my case it was linked to PHPExcel.

Before going over the project and upgrade it to PHP 7.4, I need to fix some small issues so I needed a way to test it with PHP 7.2.

I found the solution on Freek's blog and updated the content to work with PHP 7.4.

phpv() {
    valet stop
    brew unlink php@7.2 php@7.3 php
    brew link --force --overwrite $1
    brew services start $1
    composer global update
	rm -f ~/.config/valet/valet.sock
    valet install
}

alias php72="phpv php@7.2"
alias php73="phpv php@7.3"
alias php74="phpv php"

Now I can just type php72 and it'll automatically setup PHP 7.2 and then php74 to go back to PHP 7.4.