11

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.

ewok
  • 20,148
  • 51
  • 149
  • 254

9 Answers9

5

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.

Pavle Predic
  • 5,790
  • 3
  • 17
  • 17
5

There is the windows command line for PHP: http://php.net/manual/en/install.windows.commandline.php

Jonathan M
  • 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
  • 4
    I 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
3

Have a look at either running php.exe with the -a argument, or maybe the phpsh project.

  • I guess it's the closest to come to with native PHP. – hakre Dec 12 '11 at 21:17
  • 3
    phpsh 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
  • @jlafay let me know if/when you get it up and running. – ewok Jan 17 '12 at 00:08
  • 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

There is php-shell - it is not great, but still way better than php -a. (Also, it is dead simple to install, just run pear install http://jan.kneschke.de/assets/2007/2/17/PHP_Shell-0.3.1.tgz.) There is also phpa-norl, but I haven't tried it.

dimo414
  • 47,227
  • 18
  • 148
  • 244
Tgr
  • 27,442
  • 12
  • 81
  • 118
1

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".

0

I found these two on github as well:

Vijay
  • 891
  • 3
  • 19
  • 35
0

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.

Tgr
  • 27,442
  • 12
  • 81
  • 118
0

@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:

  1. 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

  2. new a Windows environment variable, key: shell.php, value: "C:\Program Files\php-5.6.12-Win32-VC11-x64"

  3. restart computer

  4. use anywhere in the system:

    php %shell.php%

Community
  • 1
  • 1
vikyd
  • 1,807
  • 1
  • 19
  • 28
-1

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.

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