No output from PHP interactive on Windows

17,553

Solution 1

The first line I type after entering php -a is slightly different to yours:

<?php          //press Enter
echo 'This ';  //press Enter
echo 'works';  //press Enter
//Press Ctrl+z on this line to get a ^Z, then press Enter
This works
C:\Windows\System32>

The nuisance is that it returns to the Windows command prompt and you have to keep typing php -a and <?php before you can type more.

To save a bit of typing I created a shortcut, right-clicked it, opened its Properties dialog and entered the following command in the Target text box:

C:\Windows\System32\cmd.exe /k "php -a"

This shortcut opens a PHP prompt in interactive mode. Alternatively, this one starts in interactive mode and lists the PHP switches:

C:\Windows\System32\cmd.exe /k "php -h & php -a"

Solution 2

Interactive mode is not the same as Interactive shell. The later accepts commands like in cmd or a shell found in linux. The first one reads in the whole script and then returns with the output. If you would hit CTRL-Z at the end of the snippet it should return the output.

Check here: http://php.net/manual/en/features.commandline.interactive.php for more information (especially the first comment).

Solution 3

If you start "php -a" and see "Interactive mode enabled" instead of "Interactive shell", then your copy of php was likely compiled without readline support, and there is no interactive shell.

As far as I know, Windows copies of PHP are all compiled without readline support. This makes the "-a" option worthless in Windows (at least to me).

"php -a" is supposed to work like this (output from a linux copy of php in this case):

$ php -a
Interactive shell
php > echo 5+8;
13
php > exit
$

"php -a" in a Windows copy of PHP is not an interactive shell, and so effectively works the same as leaving off the "-a" option. Use F6 or CTRL+Z to type the EOF character to finish the script (appears as ^Z):

C:\>php -a
Interactive mode enabled
<?php
echo 5+8;
?>
^Z
13
C:\>

Leave off the "-a" and you get essentially the same results (minus the text "Interactive mode enabled":

C:\>php
<?php
echo 5+8;
?>
^Z
13
C:\>

If you use "php -a" and get the text "Interactive mode enabled" and don't get the output for cli.prompt (usually "php >"), then you should check your php version (with "php -v") to make sure it is at least 5.1, and your php modules (with "php -m") to make sure the "readline" module is listed.

Solution 4

I make cmd script for emulate interactive php shell. It reads each command into an environment variable and then starts a new instance of PHP to run each command.

@echo off
:loop
    set /p php="php> " %=%
    php -r "%php%"
    echo.
goto loop

Solution 5

As of PHP 7.1.0 both the interactive shell and readline are supported on Windows.

Share:
17,553

Related videos on Youtube

BaliDave
Author by

BaliDave

Updated on October 11, 2022

Comments

  • BaliDave
    BaliDave less than a minute

    I'm running php interactively from xampp (5.4.7) on my Win 7 machine and cannot get any output from the window. I searched around various solutions and nothing so far has worked. Here's a sample: C:\xampp>php -v PHP 5.4.7 (cli) (built: Sep 12 2012 23:48:31) Copyright (c) 1997-2012 The PHP Group Zend Engine v2.4.0, Copyright (c) 1998-2012 Zend Technologies

    C:\xampp>php -a
    Interactive mode enabled
    <?
    echo "hi";
    printf "hi";
    fwrite (STDOUT, "hi");
    

    any other ideas???

    I also tried php -an and setting output_buffing Off in the php.ini, all to no avail.

    Basically my aim is mostly to use this as a testbed for php expressions as well as running some local scripts.

  • BaliDave
    BaliDave over 9 years
    Thanks. Ctrl-Z doesn't work, nor does ctrl-d as some suggest. But that link sounds pretty discouraging for getting an interactive php in Windows. Perhaps I need to go to Cygwin or such to do this?
  • WesleyE
    WesleyE over 9 years
    I've tried PHPSH once in combination with Cygwin, it's from Facebook and uses some Python to make it work but it did work ok after some fiddeling. phpsh.org
  • gkalpak
    gkalpak over 8 years
    Finally, something that works (on Windows 8.1) :) Thx
  • felixfbecker
    felixfbecker about 7 years
    The problem with this is it will not remember variables or functions you define
  • kurdtpage
    kurdtpage almost 5 years
    If you have interactive mode, make sure you type <?php at the start