mac使用php-version切换PHP版本

在开发过程中,有时候我们的程序对某个php版本有着极为重要的限制,特别是大型项目。

因此,我们就需要切换多个php版本来满足我们的需求。

我们使用php-version来达到这个目的。

首先我们先使用brew安装一个php5.6,为什么不使用系统原有的php5.6呢?

这个是因为有时候升级系统时,会把自带的php配置给还原,所以我们使用一个新下载的php5.6。

brew install homebrew/php/php56

下载完之后,我们先把php5.6给unlink,不然我们的php7是无法安装的。会报以下错误

Error: Cannot install homebrew/php/php70 because conflicting formulae are installed.
  php56: because different php versions install the same binaries.

Please `brew unlink php56` before continuing.

Unlinking removes a formula's symlinks from /usr/local. You can
link the formula again after the install finishes. You can --force this
install, but the build may fail or cause obscure side-effects in the
resulting software

这个时候提示我们需要执行一下

brew unlink php56

接着我们就可以下载php7了

homebrew/php/php70

下载完之后,我们就可以下载php-version了

brew install php-version

这个时候你无法直接使用php-version命令,你需要把下面这句放在~/.zshrc或者~/.bashrc的下面

source $(brew --prefix php-version)/php-version.sh && php-version 5

接着

source ~/.zshrc  或者 source ~/.bashrc

现在的话,你执行php-version就会看到有两个php版本

☁  ~  php-version
  5.6.31
* 7.0.22

带星号代表着正在使用的是php7.0,只要重启php,就能成功切换php版本咯

原文地址:https://www.cnblogs.com/ailhc/p/7398479.html