简体   繁体   English

如何在Windows的交互式CLI中运行php

[英]How to run php in interactive CLI in Windows

I would like to run PHP in a command-line-interface and I would like to get output after every line I write. 我想在命令行界面中运行PHP,我想在每行写完之后获取输出。

I have php.exe in my PATH environment variable. 我的PATH环境变量中有php.exe。 I use php -r "echo 'Hello world'" or 我使用php -r“ echo'Hello world'”或

php -a 
Interactive mode enabled 
<?php
echo "hello world"
?>
^Z

But I want a real php shell, which reacts after every command. 但是我想要一个真正的php shell,它在每个命令之后都会做出反应。 I have used something similar in Python. 我在Python中使用了类似的东西。 Is this possible in PHP? 这在PHP中可行吗? You can recommend a program that has this feature. 您可以推荐一个具有此功能的程序。

For a powerful interactive PHP CLI, I suggest you take a look at PsySH . 对于强大的交互式PHP CLI,建议您看看PsySH It's very easy to install, you just have to run one command: 它非常容易安装,您只需要运行一个命令:

composer g require psy/psysh:@stable

Then make sure you have added the composer vendor directory to the PATH system variable ( instructions ) by appending the following: 然后,通过添加以下内容,确保已将composer供应商目录添加到PATH系统变量( 指示 )中:

;C:\\Users\\[username]\\AppData\\Roaming\\Composer\\vendor\\bin\\ ; C:\\用户\\ [用户名] \\应用程序数据\\漫游\\作曲\\厂商\\ BIN \\

Then start it just run: 然后启动它即可运行:

psysh

If you want an even console better experience, I suggest you use cmder instead of the Windows Command Prompt. 如果您想要更好的控制台体验,建议您使用cmder而不是Windows命令提示符。

As of PHP 5.1.0, the CLI SAPI provides an interactive shell using the -a option if PHP is compiled with the --with-readline option. 从PHP 5.1.0开始,如果PHP是使用--with-readline选项编译的,则CLI SAPI使用-a选项提供了一个交互式shell。

Using the interactive shell you are able to type PHP code and have it executed directly. 使用交互式外壳,您可以键入PHP代码并直接执行它。

You need to run the program whith the modifier "-a" 您需要运行带有修饰符“ -a”的程序

php -a

Then, you need to recompile php whith "--with-readline" to enable this feature and works like in phyton. 然后,您需要重新编译带有“ --with-readline”的php才能启用此功能,并且在phyton中工作。

I hope I have been helpful. 希望对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM