可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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.
回答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"
回答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).
回答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.
回答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
回答5:
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:-)
回答6:
When using the Interactive Mode
, you literally need to input the whole script.
<?php
echo "HELLO WORLD";
Then to "execute" the script, you need to do CTRL+Z
on an empty line and press ENTER
回答7:
As of PHP 7.1 the interactive shell is supported on Windows.
回答8:
As you can read from the other answers, Windows PHP does not support echoing commands(easily).
On Windows 10 there is a way around this however, you can enable the Windows 10 Ubuntu sub-shell system.
To install windows-subsystem for linux
Do one of the following:
Control Panel -> Apps -> Administrate optional features.
Then make sure Windows-subsystem for linux
is checked.
You can also install it by running Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux
in an elevated Powershell Window.
Now reboot(maybe bookmark this page first)
Once you've rebooted install this app and open it.
When it's done loading and you've created your user, type sudo apt update && apt upgrade && apt install PHP
.
Now when you need php interactive just open command prompt and type bash
then php -a