How to Run PHP from Windows Command Line in Wampserver

command line locally using wamp

It might be useful to register php.exe as an ENVIRONMENT VARIABLE so the command line can recognize the 'php' command instead of needing to enter the full path '\wamp\bin\php\php5.3.8\php' as the command.

To do this, you can follow the steps outlined on this page: http://windows.fyicenter.com/view.php?ID=60. Except for Step 5, enter the path of WAMP's php.exe instead. For example, just add in $;C:\wamp\bin\php\php5.3.8 to the Variable value field. And each time you open up your command line, just run php using

php pageYouAreRunning.php 

..still keeping in mind that the pageYouAreRunning.php is relative to the current path in your command line.

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

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.

Run a php script as a background process in wamp server

  1. create a batch file to run your php script using php executable "C:\wamp\php\php.exe C:\wamp\www\index.php"
  2. add this batch file in Scheduled Task in Windows control panel.

PHP Version in Wamp and Command Line Windows not compatible

There is no Windows version of PHP so basically you must have installed a version of PHP manually i.e. not with WAMPServer.

This will probably have added an entry on you PATH ( always a bad idea when using WAMPServer anyway as you can have many versions of PHP on your system at one time with WAMPServer)

Look at your PATH and if you must, amend it to point to the PHP in \wamp\bin\php\php{version}

A better way is to remove any reference to PHP from the PATH environmant variable and write a simple batch file to add php to your path only when you want to, like this

Batch file saved in C:\windows\phppath.cmd

echo off
PATH=%PATH%;c:\wamp\bin\php\php5.4.16

Assuming you have php5.4.16 on your wampserver install.

Wampserver not changing the PHP version

The version in your terminal and the version wamp is using may be different.

In a wamp project create a PHP file and put this in it.

<?php
phpinfo();
?>

Go to the URL of the PHP file in your browser and that will show you the PHP version the apache server in wamp is using. You can change it using this tutorial.
Changing PHP version in wamp

Good luck!

php.ini - command line PHP and WAMP server access different files

Everything you have mentioned is expected behavior. Apache has it's own php.ini version it uses, and require an Apache restart to make any changes effective.

CLI will also have it's own php.ini.

Execute php commands in WAMP environment

In windows you need to set up your path environment variable (if it hasn't already been done by the installer) so that it points to the correct location for the PHP executables. Refer to the documentation for EasyPHP to see if/how you need to do this.

Then, open a dos window, and cd into the directory for your project. Then you should be able to run the commands as shown.

Trying to run PHP script from command line (possible path issue)

The solution was inside the php.ini file.

I changed
extension_dir ="c:/php7/ext/"
to
extension_dir ="c:/wamp64/bin/php/php7.2.4/ext/"



Related Topics



Leave a reply



Submit