1

I've recently installed Chocolately on Windows. I've then installed two versions of PHP (7.4.5) and (7.2.30) which I am successful in doing so. Now I am trying to figure out how I can switch between the two versions.

Is there a command to quickly switch between two versions of PHP (or any package for that matter)? For Ruby, I believe you have to install a separate package - https://chocolatey.org/packages/pik#description

Thanks in advance!

NOTE: I've installed 7.4.5 by simply executing choco install php and then 7.2.30 by executing choco install php --version 7.2.30 -my. After running both, running php -v would result in 7.4.5.

balfonso
  • 611
  • 1
  • 9
  • 17
  • I've never heard of such a tool. But if you want to have more freedom, try to run your application in a Docker container. This helps to nail down the exact requirements per project without interfering with other projects – Nico Haase May 11 '20 at 10:25
  • dig [here](https://stackoverflow.com/questions/55387799/proper-way-to-cli-php-within-wamp-wamp64-while-switching-between-multiple-php), [phpinfo](https://stackoverflow.com/a/45939248/2368696). – Adi Prasetyo May 11 '20 at 13:37
  • have you tried the answer from [so](https://stackoverflow.com/questions/15617515/windows-command-line-multiple-php-versions) [so1](https://stackoverflow.com/questions/46817384/how-to-actually-change-php-version-that-used-by-composer) ? – Adi Prasetyo May 11 '20 at 13:42

1 Answers1

1

You can switch between PHP by replacing ENV variable using below code:

# Reload Environment Function
function Reload-Env {
    $env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path","User")
}

# Set default PHP if you have installed both
$PHP = 'C:\tools\php74'; `
$Environment = ($Env:PATH.Split(';') | Where-Object -FilterScript {$_ -NotLike '*php*'}) -Join ';'; `
[System.Environment]::SetEnvironmentVariable("Path", $Environment + ';' + $PHP, "Machine"); `
Reload-Env

Note: Reload-Env function is used for reloading changed PHP in current season