Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Proper Way to CLI PHP within WAMP (WAMP64), while switching between Multiple PHP Versions on Windows 10

Used to just put the wamp php directory in my system environment variables as answered here: command line locally using wamp. Which more or less is a very simple environmental variable edit adding the WAMP servers PHP Path, such as ;C:\wamp\bin\php\php5.6.40

I'm now learning with more recent versions WAMP is recommending against doing so (as shown here: How To Run PHP From Windows Command Line in WAMPServer).

In that answer it shows a clever command line/.cmd program named phppath.cmd which is tailored to linux users.

Unedited, it looks as such:

@echo off

REM **************************************************************
REM * PLACE This file in a folder that is already on your PATH
REM * Or just put it in your C:\Windows folder as that is on the
REM * Search path by default
REM * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
REM * EDIT THE NEXT 3 Parameters to fit your installed WAMPServer
REM **************************************************************


set baseWamp=D:\wamp
set defaultPHPver=7.1.9
set composerInstalled=%baseWamp%\composer
set phpFolder=\bin\php\php

if %1.==. (
    set phpver=%baseWamp%%phpFolder%%defaultPHPver%
) else (
    set phpver=%baseWamp%%phpFolder%%1
)

PATH=%PATH%;%phpver%
php -v
echo ---------------------------------------------------------------


REM IF PEAR IS INSTALLED IN THIS VERSION OF PHP

IF exist %phpver%\pear (
    set PHP_PEAR_SYSCONF_DIR=D:\wamp\bin\php\php%phpver%
    set PHP_PEAR_INSTALL_DIR=D:\wamp\bin\php\php%phpver%\pear
    set PHP_PEAR_DOC_DIR=D:\wamp\bin\php\php%phpver%\docs
    set PHP_PEAR_BIN_DIR=D:\wamp\bin\php\php%phpver%
    set PHP_PEAR_DATA_DIR=D:\wamp\bin\php\php%phpver%\data
    set PHP_PEAR_PHP_BIN=D:\wamp\bin\php\php%phpver%\php.exe
    set PHP_PEAR_TEST_DIR=D:\wamp\bin\php\php%phpver%\tests

    echo PEAR INCLUDED IN THIS CONFIG
    echo ---------------------------------------------------------------
) else (
    echo PEAR DOES NOT EXIST IN THIS VERSION OF php
    echo ---------------------------------------------------------------
)

REM IF A GLOBAL COMPOSER EXISTS ADD THAT TOO
REM **************************************************************
REM * IF A GLOBAL COMPOSER EXISTS ADD THAT TOO
REM *
REM * This assumes that composer is installed in /wamp/composer
REM *
REM **************************************************************
IF EXIST %composerInstalled% (
    ECHO COMPOSER INCLUDED IN THIS CONFIG
    echo ---------------------------------------------------------------
    set COMPOSER_HOME=%baseWamp%\composer
    set COMPOSER_CACHE_DIR=%baseWamp%\composer

    PATH=%PATH%;%baseWamp%\composer

    rem echo TO UPDATE COMPOSER do > composer self-update
    echo ---------------------------------------------------------------
) else (
    echo ---------------------------------------------------------------
    echo COMPOSER IS NOT INSTALLED
    echo ---------------------------------------------------------------
)

set baseWamp=
set defaultPHPver=
set composerInstalled=
set phpFolder=

This sounds great, but I'm having mild success with it. How it works, is I place the phppath.cmd file into my Windows directory and it autoloads when windows starts.

The linux-windows conversions may have messed with me a little bit, but here's my edited output for Windows 10 WAMP64 (current one in my C:\Windows folder)

@echo off

REM **********************************************************************
REM * PLACE This file in a folder that is already on your PATH
REM * Or just put it in your C:\Windows folder as that is on the
REM * Serch path by default
REM * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
REM * EDIT THE NEXT 3 Parameters to fit your installed WAMPServer
REM * for example I installed WAMPServer on the D: drive you may
REM * have used C:
REM * - baseWamp : is the drive and folder where you installed WAMPServer
REM * - defaultPHPver : is the version of PHP that will be pathed
REM *                   if no Parameter is put on the bat file
REM * - composerInstalled : Where I insatlled composerInstalled
REM * - phpFolder : The folder structure that contains the Multiple
REM *               possible version of PHP I have installed
REM **********************************************************************


set baseWamp=C:\wamp64
set defaultPHPver=7.1.26
set composerInstalled=C:\Users\User\AppData\Roaming\Composer
set phpFolder=C:\wamp64\bin\php

if %1.==. (
    set phpver=C:\wamp64\bin\php\php%defaultPHPver%
) else (
    set phpver=C:\wamp64\bin\php\php
)

PATH=%PATH%;%phpver%
php -v
echo ---------------------------------------------------------------


REM IF PEAR IS INSTALLED IN THIS VERSION OF PHP

IF exist %phpver%\pear (
    set PHP_PEAR_SYSCONF_DIR=C:\wamp64\bin\php\php%phpver%
    set PHP_PEAR_INSTALL_DIR=C:\wamp64\bin\php\php%phpver%\pear
    set PHP_PEAR_DOC_DIR=C:\wamp64\bin\php\php%phpver%\docs
    set PHP_PEAR_BIN_DIR=C:\wamp64\bin\php\php%phpver%
    set PHP_PEAR_DATA_DIR=C:\wamp64\bin\php\php%phpver%\data
    set PHP_PEAR_PHP_BIN=C:\wamp64\bin\php\php%phpver%\php.exe
    set PHP_PEAR_TEST_DIR=C:\wamp64\bin\php\php%phpver%\tests

    echo PEAR INCLUDED IN THIS CONFIG
    echo ---------------------------------------------------------------
) else (
    echo PEAR DOES NOT EXIST IN THIS VERSION OF php
    echo ---------------------------------------------------------------
)

REM IF COMPOSER EXISTS ADD THAT TOO
REM **************************************************************
REM * IF A GLOBAL COMPOSER EXISTS ADD THAT TOO
REM *
REM * This assumes that composer is installed in /wamp/composer
REM *
REM **************************************************************
IF EXIST %composerInstalled% (
    ECHO COMPOSER INCLUDED IN THIS CONFIG
    echo ---------------------------------------------------------------
    set COMPOSER_HOME=C:\Users\User\AppData\Roaming\Composer
    set COMPOSER_CACHE_DIR=C:\Users\User\AppData\Roaming\Composer

    PATH=%PATH%;C:\Users\User\AppData\Roaming\Composer

    rem echo TO UPDATE COMPOSER do > composer self-update
    echo ---------------------------------------------------------------
) else (
    echo ---------------------------------------------------------------
    echo COMPOSER IS NOT INSTALLED
    echo ---------------------------------------------------------------
)

set baseWamp=
set defaultPHPver=
set composerInstalled=
set phpFolder=

Here's where my confusion is setting in.

Using the command prompt phppath in a command line I get the following output

PHP 7.1.26 (cli) (built: Jan  9 2019 21:51:32) ( ZTS MSVC14 (Visual C++ 2015) x64 )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.1.0, Copyright (c) 1998-2018 Zend Technologies
---------------------------------------------------------------
PEAR DOES NOT EXIST IN THIS VERSION OF php
---------------------------------------------------------------
COMPOSER INCLUDED IN THIS CONFIG
---------------------------------------------------------------
---------------------------------------------------------------

Additionally I cannot change my PHP version using the phppath command line.

The versions on my WAMP64 installation consist of 5.6.40 - 7.0.33 - 7.1.26 - 7.2.14 - 7.3.1

What I am trying to do (and though phppath.cmd would achieve that), is, as seen in that thread, change php CLI versions with a simple command.

phppath 5.6.40

phppath 7.0.33

etc

but these commands are ineffective, they process properly, put the command line always states PHP 7.1.26 (cli) no matter if its specified.

Did I mess up my paths or something when I tried to convert the above script from linux to Windows 10? I tried to make sure all the paths were appropriate.

edit: Using W10 x64 version WAMP 3.1.7. Read there were issues using Powershell, or rather phppath.cmd was not designed for powershell, so I'm using the program Cmder for command line functions.

edit 2: Figured out php 7.1.26 was indeed in my environmental variables.. I was only looking at USER env variables. Somehow it got into SYSTEM env variables (not completely sure how). Deleted that entry which may have been causing conflicts. Onto some more testing..

Now to just get phppath.cmd to work properly.

edit3: Last edit for now.. I'll try to mess with this more later, if anyone has any insight please provide! So as I stated the wamp php directory was in the system environmental variables, once I removed that entry the error message [ERROR C:/wamp64 or PHP in path.] in WAMP went away.

As to switch / change between PHP CLI versions -- as of right now, kind of a band-aid solution, but workable... if I just change my phppath.cmd file in my C:\Windows folder from set defaultPHPver=7.1.26 to another version like set defaultPHPver=7.0.33 or set defaultPHPver=5.6.40 --- that works to change the PHP CLI version using phppath on the commandline (AFTER a restart).

How it appears to be working is...

(manually set defaultpath in cmd file...)... Open up a CLI like cmder type in..

phppath

(here I have in my phppath.cmd a default version of 7.0.x)

PHP 7.0.33 (cli) (built: Dec  5 2018 21:22:29) ( ZTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2017 Zend Technologies
---------------------------------------------------------------
PEAR DOES NOT EXIST IN THIS VERSION OF php
---------------------------------------------------------------
COMPOSER INCLUDED IN THIS CONFIG
---------------------------------------------------------------
---------------------------------------------------------------

Then I can continue to use the default php CLI functions..

php -f version.php

Current version is PHP 7.0.33

Note: If I don't initiate by using the command phppath first I get the typical error message -- 'php' is not recognized as an internal or external command, operable program or batch file., so typing phppath is required for the php commands to work.

So this is great and workable for now. I just have to manually edit the phppath.cmd to the default version I want it to be and reset my computer.

Only issue is that, per the instructions, phppath.cmd should be able to change PHP CLI version on the fly using the commands I attempted above. This is of course better than having to manually edit the .cmd file and restart everytime I want to change the version.

Anyone have any insight as to why my Windows 10 modified phppath.cmd won't change versions via the command line, only by modifying the file and doing a computer restart? Would be much appreciated... last step to get down :)

edit: The comments got kind of long, to summarize defining %PATH% as pathBak was indeed the answer, as shown in @michael_heath Using the script below works exactly as it should on my Windows 10 machine, version changing and all.

like image 388
Brian Bruman Avatar asked Mar 27 '19 23:03

Brian Bruman


People also ask

Is there way to use two PHP versions in WAMP?

Change the 'extension_dir' setting from the first two files and open the third and make any appropriate changes. Terminate WAMP and run it again, not just restart it's services. And there you have it. Multiple PHP versions in just a click.


1 Answers

In line 20 of unedited:

set phpver=%baseWamp%%phpFolder%%1

compared to line 28 of edited:

set phpver=C:\wamp64\bin\php\php

The %1 is the variable that contains the value of the 1st script argument. The edited version is missing %1 so it does not change version, but rather uses the fixed path. This may be your main issue.

Try this edited version:

@echo off

REM ***************************************************************
REM * PLACE This file in a folder that is already on your PATH
REM * Or just put it in your C:\Windows folder as that is on the
REM * Search path by default
REM * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
REM * EDIT THE NEXT 3 Parameters to fit your installed WAMPServer
REM * for example I installed WAMPServer on the D: drive you may
REM * have used C:
REM * - baseWamp : is the drive and folder where you installed
REM *              WAMPServer
REM * - defaultPHPver : is the version of PHP that will be pathed
REM *                   if no Parameter is put on the bat file
REM * - composerInstalled : Where I installed composerInstalled
REM * - phpFolder : The folder structure that contains the Multiple
REM *               possible version of PHP I have installed
REM ***************************************************************

if not defined pathBak set "pathBak=%PATH%"

set "baseWamp=C:\wamp64"
set "defaultPHPver=7.1.26"
set "composerInstalled=%appData%\composer"
set "phpFolder=\bin\php\php"

if "%~1" == "" (
    set "phpVer=%baseWamp%%phpFolder%%defaultPHPver%"
) else if "%~1" == "dir" (
    for /d %%A in ("%baseWamp%%phpFolder%*") do echo %%~nxA
    goto :end
) else (
    set "phpVer=%baseWamp%%phpFolder%%~1"
)

set "PATH=%pathBak%;%phpVer%"
php -v
echo ---------------------------------------------------------------


REM IF PEAR IS INSTALLED IN THIS VERSION OF PHP

if exist "%phpVer%\pear" (
    set "PHP_PEAR_SYSCONF_DIR=%phpVer%"
    set "PHP_PEAR_INSTALL_DIR=%phpVer%\pear"
    set "PHP_PEAR_DOC_DIR=%phpVer%\docs"
    set "PHP_PEAR_BIN_DIR=%phpVer%"
    set "PHP_PEAR_DATA_DIR=%phpVer%\data"
    set "PHP_PEAR_PHP_BIN=%phpVer%\php.exe"
    set "PHP_PEAR_TEST_DIR=%phpVer%\tests"

    echo PEAR INCLUDED IN THIS CONFIG
    echo ---------------------------------------------------------------
) else (
    set "PHP_PEAR_SYSCONF_DIR="
    set "PHP_PEAR_INSTALL_DIR="
    set "PHP_PEAR_DOC_DIR="
    set "PHP_PEAR_BIN_DIR="
    set "PHP_PEAR_DATA_DIR="
    set "PHP_PEAR_PHP_BIN="
    set "PHP_PEAR_TEST_DIR="

    echo PEAR DOES NOT EXIST IN THIS VERSION OF php
    echo ---------------------------------------------------------------
)

REM IF A GLOBAL COMPOSER EXISTS ADD THAT TOO
REM **************************************************************
REM * IF A GLOBAL COMPOSER EXISTS ADD THAT TOO
REM *
REM * This assumes that composer is installed in /wamp/composer ?
REM *
REM **************************************************************
if exist "%composerInstalled%" (
    echo COMPOSER INCLUDED IN THIS CONFIG
    echo ---------------------------------------------------------------
    set "COMPOSER_HOME=%composerInstalled%"
    set "COMPOSER_CACHE_DIR=%composerInstalled%"

    set "PATH=%PATH%;%composerInstalled%"

    rem echo TO UPDATE COMPOSER do > composer self-update
    echo ---------------------------------------------------------------
) else (
    echo ---------------------------------------------------------------
    echo COMPOSER IS NOT INSTALLED
    echo ---------------------------------------------------------------
)

:end
set "baseWamp="
set "defaultPHPver="
set "composerInstalled="
set "phpFolder="
  • Used paths from your edited version.
  • Your hard coded paths replaced with existing variables.
  • %phpFolder% changed to relative path, which appended to %baseWamp% in usage (like unedited version).
  • Composer path changed to %appData%\composer which should match your hard coded path.
  • Fixed paths in Pear section by removing the leading %baseWamp%\bin\php\php.
  • Undefine Pear variables if not exist in selected PHP version.
  • Added %pathBak% to store original PATH value, so the original PATH value can be reused if the script is run again in the same session.
  • Added argument dir. Will list the folders names so you can see what versions are available.
like image 98
michael_heath Avatar answered Oct 04 '22 03:10

michael_heath