php-switch is small utility designed to help Homebrew users to either switch
the default PHP version on their system or to run scripts with a specific PHP
version. It is meant to be used with the
shivammathur/php
tap.
php-switch is distributed as a Homebrew formula, which you can install by running the following command.
brew install nhedger/php-switch/php-switch
Finally, initialize it by adding the following line anywhere in your ~/.zshrc
file.
source php-switch
Duing initialization, php-switch will look for installed PHP versions in your Homebrew Cellar. Any installed PHP version will be made available to you in your shell as functions named after the PHP version.
To switch to a PHP version, simply type its version number (omitting the patch number). For example, to switch to PHP 8.1 run the following.
8.0
Once the PHP version has been switched, its version will be printed out.
You may not always want to switch your default PHP version but still need to
quickly run a script under a specific PHP version. To do that, prefix your PHP
script with the version you want to run it with. For example, to run test.php
under PHP 7.4, run the following.
7.4 test.php
This utility script is open-sourced software distributed under the MIT License.