Is there a command line php shell available for windows? I looking for something similar to the python shell, that will allow me to open and immediately begin executing code.
9 Answers
Try this:
<?php
$fh = fopen('php://stdin', 'r');
$cmd = '';
$bcLvl = 0;
while (true)
{
$line = rtrim(fgets($fh));
$bcLvl += substr_count($line, '{') - substr_count($line, '}');
$cmd.= $line;
if ($bcLvl > 0 or substr($cmd, -1) !== ';')
continue;
eval($cmd);
$cmd = '';
}
Save this code to a file (eg shell.php) and then run it from console:
php shell.php
Hit CTRL+C to exit.

- 5,790
- 3
- 17
- 17
There is the windows command line for PHP: http://php.net/manual/en/install.windows.commandline.php

- 17,145
- 9
- 58
- 91
-
How does it behave like python shell under windows? I think it's really different in how interactivity (typing) is supported compared to what the OP asks for. – hakre Dec 12 '11 at 21:17
-
4I attempted running `php -a` but it did not perform the way I expected it to. I did not get a prompt of any kind (other than a note saying "Interactive mode enabled"), and a simple `echo "hello world"` does not produce any kind of output. – ewok Dec 12 '11 at 21:35
-
See the first user-contributed note here for an example: http://www.php.net/manual/en/features.commandline.interactive.php – Jonathan M Dec 12 '11 at 21:43
-
IIRC the python shell executes on `ENTER` not on `CTRL+Z` and then pressing `ENTER`. You can probably create yourself a keyboard makro with a tool like autohotkey that does so (e.g. on `SHIFT+ENTER`) and then re-starting `php -a`. – hakre Dec 12 '11 at 22:46
-
@hakre, yes. It's not going to be exactly like python, but at least it's interactive. Good note. – Jonathan M Dec 12 '11 at 22:48
-
AFAIK readline support is required for php -a to work, so in other words, php -a may NOT work, as is the case with me and the OP. – Jonathan Dec 02 '14 at 23:04
-
The PHP runtime is absolutely horrible as a REPL, even with `-a`. It has no auto-print, it exits on certain errors (although I think that has improved in more recent versions), no integrated help etc. – Tgr Mar 16 '15 at 15:54
Have a look at either running php.exe with the -a
argument, or maybe the phpsh project.
-
-
3phpsh seems to be what I am looking for, but I can't seem to get it running on windows. when I run `python setup.py build` I get `NameError: name '__init__' is not defined` – ewok Dec 13 '11 at 13:55
-
1@ewok, I struggled with getting phpsh to work on windows too. I've hacked at the python code to attempt to get it to work and it is definitely written for linux. There are a lot of linux/unix specific os methods used. I'm thinking of forking the project to make it windows friendly since I know python. – Jeff LaFay Jan 16 '12 at 13:37
-
-
1@ewok, I most likely won't remember to come back here and comment to let you know so it's best if you "watch" the fork on github instead so you can receive notifications. https://github.com/jefflafay/phpsh – Jeff LaFay Jan 17 '12 at 01:18
-
AFAIK readline support is required for php -a to work, so in other words, php -a may NOT work, as is the case with me and the OP. – Jonathan Dec 02 '14 at 23:05
-
The PHP runtime is absolutely horrible as a REPL, even with `-a`. It has no auto-print, it exits on certain errors (although I think that has improved in more recent versions), no integrated help etc. phpsh is better but written in Python so somewhat troublesome to install on Windows. – Tgr Mar 16 '15 at 15:55
-
1
-
https://github.com/rainchen/phpa-norl as a casual google search would have discovered. – Tgr Dec 03 '14 at 22:53
Another simple variant, influenced by other answers. Create and run the following cmd-script:
@echo off
:loop
php.exe -r "while (true) { eval (fgets (STDIN) ); echo PHP_EOL; }"
goto loop
Immediate execution, Ctrl+C for exit. Insert correct path before "php.exe".
I found these two on github as well:
ubermuda/PHPInteractiveShell https://github.com/ubermuda/PHPInteractiveShell
d11wtq/boris · non windows https://github.com/d11wtq/boris

- 891
- 3
- 19
- 35
PsySH is the best REPL shell I have seen recently. Tab-completion, phpdoc support, proper namespace handling, interactive debugging, plugin support and many more. It is pure PHP and has composer support so it should be easy to install on Windows. I have only tried it on Linux but looking at the code, it seems to even have some history support on OSes without readline.

- 27,442
- 12
- 81
- 118
@Pavle Predic 's answer (https://stackoverflow.com/a/15742101/2752670) works for me on Windows.
But I want to use the shell.php
file anywhere, so my solution is:
new and save
shell.php
in php installed dir (or where else you like), e.g.C:\Program Files\php-5.6.12-Win32-VC11-x64\shell.php
new a Windows environment variable, key:
shell.php
, value:"C:\Program Files\php-5.6.12-Win32-VC11-x64"
restart computer
use anywhere in the system:
php %shell.php%
-
I had to comment "Restart computer" -- it's very rare you actually need to do that. – Paul Allsopp Sep 18 '17 at 22:19
Maybe answered - Read here: https://stackoverflow.com/a/19085620/2480481 I think i found easiest way to do it because im so interested to use myself :)
I try all options from here and from some other forums/pages... Windows cannot use readline.