当前位置: 动力学知识库 > 问答 > 编程问答 >

No output from PHP interactive on Windows

问题描述:

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.

网友答案:

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"

网友答案:

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

网友答案:

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.

网友答案:

To use in windows. Ensure you have a PATH variable to php directly in your windows environment. May require restart. Use gitbash/cmd/rails shell/nodejs etc. but shouldn't make any difference. Open cmd

Check php with command: php -v if all good populates the php version etc etc

load interactive mode with: php -a enter your code once enabled, for example as follows (each line has carriage return) the ^z is the shell output of pressing the ctrl key and the z key together this runs your small snippet but instantly pops you out of php interactive mode

<?php
$x = 5;
$y = 6;
$z = $x + $y;
echo $z;
?>
^z

(will return 11) was really good at maths ha:-)

网友答案:

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
分享给朋友:
您可能感兴趣的文章:
随机阅读: