How can I easily switch between PHP versions on Mac OSX?



PHP Snippet 1:

brew unlink [email protected]
brew link [email protected]

PHP Snippet 2:

$ sphp 7.0 => PHP 7.0
$ sphp 7.3 => PHP 7.3
$ sphp 7.4 => PHP 7.4

PHP Snippet 3:

brew unlink [email protected]
brew install [email protected]
brew link [email protected]

PHP Snippet 4:

brew link [email protected] --force

PHP Snippet 5:

$ php -v

PHP Snippet 6:

$ brew unlink php55
$ brew install php70

PHP Snippet 7:

brew switch php56 5.6.17

PHP Snippet 8:

> brew list | grep php
#output
php56
php56-intl
php56-mcrypt
php71
php71-intl
php71-mcrypt

PHP Snippet 9:

> switch-php 71 # or switch-php 56
#output
PHP version [71] found
Switching from [php56] to [php71] ... 
Unlink php56 ... [OK] and Link php71 ... [OK]
Updating Apache2.4 Configuration /usr/local/etc/httpd/httpd.conf ... [OK]
Restarting Apache2.4 ... [OK]
PHP 7.1.11 (cli) (built: Nov  3 2017 08:48:02) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.1.0, Copyright (c) 1998-2017 Zend Technologies

PHP Snippet 10:

vi /usr/local/etc/httpd/httpd.conf

PHP Snippet 11:

LoadModule php7_module /usr/local/Cellar/php71/7.1.11_22/libexec/apache2/libphp7.so
#LoadModule php5_module /usr/local/Cellar/php56/5.6.32_8/libexec/apache2/libphp5.so

PHP Snippet 12:

   brew install brew-php-switcher

PHP Snippet 13:

% brew search php
brew-php-switcher   php-cs-fixer        [email protected]             phplint             phpstan             pup
php ?               php-cs-fixer@2      [email protected] ?           phpmd               phpunit
php-code-sniffer    [email protected]             phpbrew             phpmyadmin          pcp

% brew unlink php
% brew link [email protected]

PHP Snippet 14:

brew link --overwrite [email protected]

PHP Snippet 15:

brew unlink php
brew install [email protected]
brew link [email protected]

PHP Snippet 16:


switchphp7() {
    valet stop
    brew unlink [email protected]
    brew services stop [email protected]
    brew link --force --overwrite [email protected]
    brew services start [email protected]
    composer global update
    rm -f ~/.config/valet/valet.sock
    valet install
    valet start
}


switchphp8() {
    valet stop
    brew unlink [email protected]
    brew services stop [email protected]
    brew link --force --overwrite [email protected]
    brew services start [email protected]
    composer global update
    rm -f ~/.config/valet/valet.sock
    valet install
    valet start
}

PHP Snippet 17:

switchphp8