81

I'm new to php and wanted to run php from command line. I have installed WAMP and set the "System Variables" to my php folder ( which is C:\wamp\bin\php\php5.4.3).

When i go to Run -> CMD -> Type php -a and hit enter, it says interactive mode enabled. But when I write echo 'Hi'; it shows nothing.

I even don't see anything like 'php >" when i type php -a and hit enter.

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
Daman
  • 933
  • 1
  • 7
  • 5
  • http://stackoverflow.com/questions/7361149/command-line-localy-using-wamp – mkjasinski Mar 24 '13 at 09:56
  • As far as I can tell the only proper option for Windows is to use the excellent [psysh](http://psysh.org/), recommended from this other SO answer: https://stackoverflow.com/a/33790472/327074 – icc97 Oct 27 '17 at 15:23

12 Answers12

78

The PHP CLI as its called ( php for the Command Line Interface ) is called php.exe It 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.z
php -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 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.4.3
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=%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 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=
set phpver=
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
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
  • 1
    It works, thanks. Can you explain how this works? I see the path to php folder but how does that associate it with command line variable "php", and what does that PATH=%PATH% do? – TetraDev Nov 09 '15 at 21:09
  • `PATH=%PATH%` says create a new PATH environmant variable with the current path i.e. `%PATH%` plus the new directory we want to add to it. Windows searches folders in the PATH to find executables i.e. 'php.exe' that it cannot find in the current working directory. – RiggsFolly Nov 10 '15 at 09:13
  • The OP was effectively asking for a PHP REPL in Windows. I don't think this answers the question – icc97 Oct 27 '17 at 15:25
  • 1
    @Cullub Not working in Power Shell - in normal shell it works –  Jun 09 '18 at 18:32
  • @Cullub Hi, no its not written to be used in PowerShell – RiggsFolly Jun 18 '18 at 17:42
  • Please note the PEAR env variables have wrong path set, should not have hardcoded part d:/wamp/bin/php/php. Btw, excellent script, understood earlier mistakes. – Zimmi Jan 24 '21 at 18:13
  • would be great if there was a way to "switch from default" so that i didnt need to run this script every time i open a git bash window. – liquidcms Nov 17 '21 at 17:08
  • I don't understand the .==. but form me that test always passes and i always get the default PHP – liquidcms Nov 25 '21 at 22:24
  • @liquidcms The `.==.` tests if a parameter was entered. To make it use a specific version 1) that version must be installed in WAMPServer and 2) you run it like this `> phppath 8.1.0` where `8.1.0` is the version number that matches a folder name so the folder would be `c:\wamp64\bin\php\php8.1.0` for example – RiggsFolly Nov 26 '21 at 09:12
  • Yes, was my bad as i posted below. My issue was i was trying to run this in git bash so added a wrapper to eval this .bat; but wasn't passing in the arg. So each time it was seen as blank - hence the default. But turns out none of this works for me anyway as i need to use it for composer; and setting path in git bash shell doesn't impact the version which composer uses. :( – liquidcms Nov 26 '21 at 18:26
37

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

yprez
  • 14,854
  • 11
  • 55
  • 70
O.O
  • 7,179
  • 1
  • 34
  • 32
  • PHP 7.1 has `readline` enabled in the windows build. Doing `php -a` will take you to the `Interactive Shell` and allow the execution of commands as you hit enter, no need to type ` – superphonic Feb 26 '18 at 11:34
12

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"

alex
  • 479,566
  • 201
  • 878
  • 984
Nishchit
  • 18,284
  • 12
  • 54
  • 81
  • 1
    With WAMPServer you should never add anything to the path. It destroys the ability to easily switch between multiple versions of PHP – RiggsFolly Sep 02 '15 at 13:00
11

The problem you are describing sounds like your version of PHP might be missing the readline PHP module, causing the interactive shell to not work. I base this on this PHP bug submission.

Try running

php -m

And see if "readline" appears in the output.

There might be good reasons for omitting readline from the distribution. PHP is typically executed by a web server; so it is not really need for most use cases. I am sure you can execute PHP code in a file from the command prompt, using:

php file.php

There is also the phpsh project which provides a (better) interactive shell for PHP. However, some people have had trouble running it under Windows (I did not try this myself).

Edit: According to the documentation here, readline is not supported under Windows:

Note: This extension is not available on Windows platforms.

So, if that is correct, your options are:

  • Avoid the interactive shell, and just execute PHP code in files from the command line - this should work well
  • Try getting phpsh to work under Windows
driis
  • 161,458
  • 45
  • 265
  • 341
  • No it does not appear? What does it means? – Daman Mar 24 '13 at 10:05
  • It means that the interactive mode does not work in your PHP distribution. I think you are best off putting the code in .php files and execute those from the command line or via a web server - that should work. The link in my answer mentions that you could compile PHP from source and include `readline` to get this to work - but that could be a little bit of an endeavour on Windows if you don't have the experience needed and the whole toolchain installed. – driis Mar 24 '13 at 10:07
  • @driis I had installed python to use phpsh from Facebook but after lots of research i found that phpsh is not compatible with python 3 :( Anything same like phpsh that is supported by python 3? – Daman Mar 24 '13 at 11:54
  • @Daman, sorry, I am not aware of anything. Perhaps install Python 2 ? Also, I think people has been having issues running phpsh under Windows too, again, I did not try it myself. – driis Mar 24 '13 at 12:01
10

If you want to just run a quick code snippet you can use the -r option:

php -r "echo 'hi';"

-r allows to run code without using script tags <?..?>

James
  • 557
  • 7
  • 16
4

You can run php pages using php.exe create some php file with php code and in the cmd write "[PATH to php.ext]\php.exe [path_to_file]\file.php"

Ram Sharma
  • 8,676
  • 7
  • 43
  • 56
Adidi
  • 5,097
  • 4
  • 23
  • 30
2

UPDATED After few research, best solution was to use that info another stackoverflow thread to avoid ctrl+z input and also from the scree output. So, instead of php -a you should use call "php.exe" -f NAMED_SCRIPT.php

OLD Readline not possible under Windows, so none of existent php shells written in php will work. But there's a workaround using -a interactive mode.

2 commmon problems here. You cannot see result until executes CTRL Z command to indicate the final of code/file like EOF. When you do, result in most cases is printed result and fast closed window. Anyway, you will be returned to cmd not the -a interactive mode.

Save this content into a .bat file, and define your PHP PATH into Windows variables, or modify php.exe to "full path to exe" instead:

::
:: PHP Shell launch wrapper
::
@ECHO off
call "php.exe" -a

echo.
echo.

call "PHP Shell.bat"

This is a simple Batch launching -a mode of php.exe. When it launchs php, stop script even no pause is wrote because is "into" the interactive waiting for input. When you hit CTRL Z, gets the SIGSTEP (next step) not the SIGSTOP (close, CTRL+C usually), then read the next intruction, wich is a recursive call to .bat itself. Because you're always into PHP -a mode, no exit command. You must use CTRL+C or hit the exit cross with mouse. (No alt+f4)

You can also use "Bat to Exe" converter to easy use.

Community
  • 1
  • 1
m3nda
  • 1,986
  • 3
  • 32
  • 45
0

That is because you are in 'Interactive Mode' where php evaluates everything you type. To see the end result, you do 'ctrl+z' and Enter. You should see the evaluated result now :)

p.s. run the cmd as Administrator!

saan
  • 1
0

The following solution is specifically for wamp environments:

This foxed me for a little while, tried all the other suggestions, $PATH etc even searched the windows registry looking for clues:

The GUI (wampmanager) indicates I have version 7 selected and yes if I phpinfo() in a page in the browser it will tell me its version 7.x.x yet php -v in the command prompt reports a 5.x.x

If you right click on the wampmanager head to icon->tools->delete unused versions and remove the old version, let it restart the services then the command prompt will return a 7.x.x

This solution means you no longer have the old version if you want to switch between php versions but there is a configuration file in C:\wamp64\wampmanager.conf which appears to specify the version to use with CLI (the parameter is called phpCliVersion). I changed it, restarted the server ... thought I had solved it but no effect perhaps I was a little impatient so I have a feeling there may be some mileage in that.

Hope that helps someone

BuddhaLincoln
  • 109
  • 1
  • 1
  • 1
    The old version as you call it is the version of PHP that all of WAMPServer's own internal scripts are tested against. If you remove PHP5.x.x means you run the risk of these scripts not working! – RiggsFolly Oct 27 '17 at 23:10
0

just do these steps if you don't need your old php version:

  • open wamp and right click on wamp manager than go : tools/Change PHP CLI Version than change php version to latest
  • another time right click on wamp manager than go : tools/Delete unuserd versions and delete the oldest version which your system insist on it to be your pc php version :D
  • go to control panel/user account/change my environment variables and in PATH variable click edit and add your latest php version path which is in your wamp server bin folder
  • close all command lines or IDEs and restart them and check for php -v

this works well

Mohad Hadi
  • 1,826
  • 3
  • 24
  • 39
  • Never add the PHP folder to the PATH env variable in WAMPServer. Every time you upgrade or switch the PHP version you will forget you did this and then have to hunt for this to amend it – RiggsFolly Mar 02 '21 at 10:26
-1

In windows, put your php.exe file in windows/system32 or any other system executable folders and then go to command line and type php and hit enter following it, if it doesnt generate any error then you are ready to use PHP on command line. If you have set your php.exe somewhere else than default system folders then you need to set the path of it in the environment variables! You can get there in following path....

control panel -> System -> Edith the environment variables of your account -> Environment Vaiables -> path -> edit then set the absolute path of your php.exe there and follow the same procedure as in first paragraph, if nothing in the error department, then you are ready to use php from command line!

user3308965
  • 41
  • 1
  • 6
  • Never put PHP on the PATH when using WAMPServer. WAMPServer can have many verison of PHP installed. Putting one version in the PTH means you cannot switch between versions. – RiggsFolly Oct 27 '17 at 23:11
-1

A slight improvement on RiggsFolly's script above, if you set:

PATH=%phpver%;%PATH%

and add your new PHP ver path at the beginning; this allows you to set a default path in your Environment setting and then you only need this script when you want to change to a different version.

Also, if like me, you want to run this in a git bash shell, just call make a bash script to call the .bat file:

#!/bin/bash
eval phppath.bat $1
liquidcms
  • 301
  • 1
  • 3
  • 17