'Windows Command Line - Multiple PHP Versions

I am currently running Wampserver with multiple PHP versions (5.3.8, 5.4.3). Wampserver easily allows you to switch back and forth between the php version you want apache to use. However, I'm having trouble dealing with multiple versions from the CLI. I have projects that require the command line, but some are compatible with php5.4, while some are not.

Is there a way to create some type of "alias" in Windows that allows me to specify which version of PHP to use on the command line .. I.E: "php54 cache:clear", "php53 cache:clear" ??

Thanks!



Solution 1:[1]

Its quite simple really.

Create yourself a batch file to add php to the path, place that file in one of the folders that is currently on your PATH so you can run it from anywhere. Then whenever you are in any folder that has CLI scripts in it run the batch file

Example batch file:

Lets call it PHPPATH.CMD

path=%path%;c:\wamp\bin\php\php5.4.11
php -v

Now if you want to use another version of the PHP CLI just change the batch file or if you are feeling clever make the batch file accept a paramter so you can specify the version of php you want on the path.

Solution 2:[2]

If you are able to do so, just create aliases:

alias 'php56'='/Path/To/php5.6/bin/php'; alias 'php55'='/Path/To/php5.5/bin/php';

If not, create links in a seperate directory that point to the binaries.

php56 is a link to /Path/To/php5.6/bin/php

php55 is a link to /Path/To/php5.5/bin/php

put these into /Seperate/Path/ and add '/Seperate/Path/' to %PATH%

Hope this helps

Solution 3:[3]

I've searched how to do it really easily without changing the environment variable path with the command line (because it's character limited) and find this solution (only on windows 10) : in your environment variable path , change [D:\wamp64\bin\php\"your php version"] in [D:\wamp64\bin\php\php]

then create a junction between your php version directory and D:\wamp64\bin\php\php by running this command in cmd as administrator : mklink /J D:\wamp64\bin\php\php D:\wamp64\bin\php\php"your php version"

then you can use this batch file

echo off
::read the actual php version
::(read the second word of php -v output )
for /f "tokens=2" %%i in ('php -v') do (

    ::if actual version is 7.4.0 I want 7.4.1
    if "%%i"=="7.4.0" set phpV="7.4.1"
    ::if actual version is 7.4.1 I want 7.4.0
    if "%%i"=="7.4.1" set phpV="7.4.0"
)

::delete the old junction
rmdir "D:\wamp64\bin\php\php"

::create a new one to the version I want
::!!!!!!!!!!!!!!!!!!! paths must not contains spaces
mklink /J D:\wamp64\bin\php\php D:\wamp64\bin\php\php%phpV%

now you can run your batch file in your cmd and don't even have to restart it after.

Solution 4:[4]

Instead of setting up the permanent paths of different PHP versions, we can use absolute paths to access different PHP versions from the windows command line:

PHP 5.6.40

C:\wamp64\www\test>php -v
PHP 5.6.40 (cli) (built: Jan  9 2019 15:10:36)
Copyright (c) 1997-2016 The PHP Group
    Zend Engine v2.6.0, Copyright (c) 1998-2016 Zend Technologies
    with Xdebug v2.5.5, Copyright (c) 2002-2017, by Derick Rethans

PHP 8.0.15

C:\wamp64\www\test>C:\wamp64\bin\php\php8.0.15\php -v
PHP 8.0.15 (cli) (built: Jan 18 2022 13:47:36) ( ZTS Visual C++ 2019 x64 )
Copyright (c) The PHP Group
Zend Engine v4.0.15, Copyright (c) Zend Technologies
    with Zend OPcache v8.0.15, Copyright (c), by Zend Technologies
    with Xdebug v3.1.2, Copyright (c) 2002-2021, by Derick Rethans

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 dominikmees
Solution 3 Rohit
Solution 4