php 如何在PHP交互模式下执行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17391811/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 12:49:03  来源:igfitidea点击:

How to execute in PHP interactive mode

phpcommand-line

提问by Abhishek Mhatre

I am new to PHP, and I just wanted to run PHP in a command prompt (interactive mode).

我是 PHP 新手,我只想在命令提示符(交互模式)中运行 PHP。

So I typed this code:

所以我输入了这个代码:

`php -v`
`<?php`
`echo 7;`
`?>`

But I do not know how to execute it, because when I press Entercmd expects me to continue writing the code. I searched on php.net and some other forums, but I didn't find anything. So is there a function key or something to display the result?

但我不知道如何执行它,因为当我按下Entercmd时,我希望我继续编写代码。我在 php.net 和其他一些论坛上搜索过,但没有找到任何东西。那么是否有功能键或其他东西来显示结果?

采纳答案by jayhendren

Ctrl+dwill trigger an end-of-file condition and cause the script to be executed.

Ctrl+d将触发文件结束条件并导致脚本被执行。

See also How does the interactive php shell work?

另请参阅交互式 php shell 如何工作?

Interactive Shell and Interactive Mode are not the same thing, despite the similar names and functionality.

If you type php -a and get a response of 'Interactive Shell' followed by a php> prompt, you have interactive shell available (PHP was compiled with readline support). If instead you get a response of 'Interactive mode enabled', you DO NOT have interactive shell available and this article does not apply to you.

So if you get only "Interactive mode enabled", then you'll only be able to type in PHP code and then when you're done, send PHP an EOF to execute it.

尽管名称和功能相似,但 Interactive Shell 和 Interactive Mode 并不是一回事。

如果您键入 php -a 并得到“Interactive Shell”的响应,然后是 php> 提示,则您有可用的交互式 shell(PHP 是使用 readline 支持编译的)。相反,如果您收到“启用交互模式”的响应,则您没有可用的交互式 shell,本文不适用于您。

因此,如果您只获得“启用交互模式”,那么您将只能输入 PHP 代码,然后当您完成后,向 PHP 发送一个 EOF 以执行它。

"EOF" means "end-of-file".

“EOF”的意思是“文件结尾”。

回答by Josiah

If following your command with Ctrl+ Disn't working, you probably don't have Interactive Shell(which apparently gives input after each line). That requires compiling phpwith special options. But if php -asays interactive mode, you can type directly into it and send Ctrl+ Zat the end of the file.

如果用Ctrl+跟随你的命令D不起作用,你可能没有Interactive Shell(这显然在每一行之后给出输入)。这需要php使用特殊选项进行编译。但是如果php -ainteractive mode,您可以直接输入并在文件末尾发送Ctrl+ Z

php -a
interactive mode enabled

<?php
echo "hello there world!";
phpinfo();
?>
^Z

(Hit enter)

(命中enter

hello there world!
phpinfo....

There's more information in the comments in the PHP comments.

PHP 注释中的注释中有更多信息。