How To Run PHP From Windows Command Line in WAMPServer How To Run PHP From Windows Command Line in WAMPServer windows windows

How To Run PHP From Windows Command Line in WAMPServer


The PHP CLI as its called ( php for the Command Line Interface ) is called php.exeIt lives in c:\wamp\bin\php\php5.x.y\php.exe ( where x and y are the version numbers of php that you have installed )

If you want to create php scrips to run from the command line then great its easy and very useful.

Create yourself a batch file like this, lets call it phppath.cmd :

PATH=%PATH%;c:\wamp\bin\php\phpx.y.zphp -v

Change x.y.z to a valid folder name for a version of PHP that you have installed within WAMPServer

Save this into one of your folders that is already on your PATH, so you can run it from anywhere.

Now from a command window, cd into your source folder and run >phppath.

Then run

php your_script.php

It should work like a dream.

Here is an example that configures PHP Composer and PEAR if required and they exist

@echo offREM **************************************************************REM * PLACE This file in a folder that is already on your PATHREM * Or just put it in your C:\Windows folder as that is on theREM * Search path by defaultREM * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -REM * EDIT THE NEXT 3 Parameters to fit your installed WAMPServerREM **************************************************************set baseWamp=D:\wampset defaultPHPver=7.4.3set composerInstalled=%baseWamp%\composerset phpFolder=\bin\php\phpif %1.==. (    set phpver=%baseWamp%%phpFolder%%defaultPHPver%) else (    set phpver=%baseWamp%%phpFolder%%1)PATH=%PATH%;%phpver%php -vecho ---------------------------------------------------------------REM IF PEAR IS INSTALLED IN THIS VERSION OF PHPIF exist %phpver%\pear (    set PHP_PEAR_SYSCONF_DIR=%baseWamp%%phpFolder%%phpver%    set PHP_PEAR_INSTALL_DIR=%baseWamp%%phpFolder%%phpver%\pear    set PHP_PEAR_DOC_DIR=%baseWamp%%phpFolder%%phpver%\docs    set PHP_PEAR_BIN_DIR=%baseWamp%%phpFolder%%phpver%    set PHP_PEAR_DATA_DIR=%baseWamp%%phpFolder%%phpver%\data    set PHP_PEAR_PHP_BIN=%baseWamp%%phpFolder%%phpver%\php.exe    set PHP_PEAR_TEST_DIR=%baseWamp%%phpFolder%%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 TOOREM **************************************************************REM * IF A GLOBAL COMPOSER EXISTS ADD THAT TOOREM *REM * This assumes that composer is installed in /wamp/composerREM *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=

Call this command file like this to use the default version of PHP

> phppath

Or to get a specific version of PHP like this

> phppath 5.6.30


I remember one time when I stumbled upon this issue a few years ago, it's because windows don't have readline, therefore no interactive shell, to use php interactive mode without readline support, you can do this instead:

C:\>php -a Interactive mode enabled <?php echo "Hello, world!"; ?> ^Z Hello, world!

After entering interactive mode, type using opening (<?php) and closing (?>) php tag, and end with control Z (^Z) which denotes the end of file.

I also recall that I found the solution from php's site user comment: http://www.php.net/manual/en/features.commandline.interactive.php#105729


Try using batch file

  1. Open notepad
  2. type php -S localhost:8000
  3. save file as .bat extension, server.bat
  4. now click on server.bat file your server is ready on http://localhost:8000

Dependency

if you got error php not recognize any internal or external command then goto environment variable and edit path to php.exe "C:\wamp\bin\php\php5.4.3"