How to Switch Between PHP Versions on Your Mac 🔀

Jalen Davenport
2 min readMar 23, 2018

--

If only it was as simple as plugging and unplugging cables!

If you do development work on a Mac, chances are you’re using Homebrew to install and maintain the various tools, languages, and packages that make up your local development environment. One of the most common things folks use Brew to install is PHP. Homebrew makes it very easy to install any PHP versions you’re interested in along with any extensions your environment may require. But here’s where one of the biggest Brew/PHP pain points appears: there’s no simple out-of-the-box method of managing multiple PHP versions and switching between them in Brew. We’ve all got those legacy projects that won’t work on newer versions of PHP, but at the same time we want to (or have to) use a newer version for any newer development projects! Because of this, people generally end up turning to other options (like MAMP) that provide better options for switching between the currently installed PHP versions.

This pain-point is why I built my newest side-project, switch-php; a simple command-line script that we can use to switch PHP with one small command.

There are currently several methods of installation:

  1. You can install with NPM: $ npm install --global switch-php
  2. You can install with Yarn: $ yarn global add switch-php
  3. You can manually download the switch-php.sh file from the switch-php repo and move it into your home directory. Then you’ll need to add this line in your .bashrc/.zshrc/etc.: source ~/switch-php.sh

Once you install it via one of the above methods, you will be able to access switch-php globally.

Here’s an bare-minimum example of how to use it:

$ switch-php 72

Here’s what we’re doing:

  1. switch-php→ The main command. (Required)
  2. 72 → Specify the version of PHP you want to switch too, in this case we’re asking for php72. (Required)

However, you can also pass some optional parameters that provide some additional functionality. For example:

$ switch-php 71 -v -m 512M 
  • -v → Request verbose output (Optional)
  • -m 512M → Request a custom PHP memory setting. If you don't pass an additional argument, the memory will be reset to the Valet default. (Optional)

Please download switch-php, give it a spin, and let me know your thoughts! Any bugs or feature requests can be added to the Github repo. If you found switch-php useful in your workflow, please share it with your friends and help them enjoy Brewing PHP again! Thanks!

As always, if you found this post helpful or enjoyed the read, please follow me here or on Twitter to see future posts!

--

--