php 如何从终端执行php块而不保存到文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7669906/
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
How to execute php block from terminal without saving to a file
提问by Alex Eftimiades
Say I have a block of code I would like to test like this:
假设我有一个代码块,我想像这样测试:
<?php
Print "Hello, World!";
?>
How I quickly run this code from terminal without saving it to a file?
如何从终端快速运行此代码而不将其保存到文件?
I tried things like...
我尝试了诸如...
php -r "Print "Hello, World!";"
but just got complaints about syntax errors. There has to be a simple way of doing this. I just have yet to find any explanations.
但刚刚收到有关语法错误的投诉。必须有一个简单的方法来做到这一点。我只是还没有找到任何解释。
回答by Mehdi Hosseini
for quick access to PHP in Terminal at first install PHP and then run this :
为了在终端中快速访问 PHP,首先安装 PHP,然后运行:
php -a
Details:
详情:
php -a
opens a interactive shell for type directly php commands and view the result Immediately, for example after type php -a
in linux shell you can type echo 'Hello World';
and after Press Enter Hello World!
will be printed on screen.
php -a
打开一个交互式 shell,直接输入 php 命令并立即查看结果,例如php -a
在 linux shell 中键入echo 'Hello World';
后,您可以键入,按 EnterHello World!
后将打印在屏幕上。
Windows Solution
视窗解决方案
in windows there is no interactive mode same as Linux cause windows can't read lines from command line, but still can use interactive like mode!, so on windows open php on place you installed it for example if you use xampp php is on C:\xampp\php
and then type php -a
like what you type in terminal but on end of each part you want to view the results just press Ctrl+Z
and then Press enter.
在窗户那里,Linux的原因Windows不能读取命令行线,但仍然可以安装它,例如,如果你用的是XAMPP PHP是对地方采用互动模式一样!所以在Windows中打开PHP是无交互模式相同C:\xampp\php
,并然后键入php -a
您在终端中键入的内容,但在每个部分的末尾,您要查看结果只需按Ctrl+Z
然后按 Enter。
php -a
echo 'hello world!';
^Z
回答by alex
Escape the inside double quotes ("
) that you are using to delimit your string.
转义"
用于分隔字符串的内部双引号 ( )。
php -r "Print \"Hello, World!\";"
Alternatively, use single quotes ('
) for the PHP string or for the quoting of the PHP code.
或者,'
对 PHP 字符串或对 PHP 代码的引用使用单引号 ( )。
If you run php --help
you can see a list of commands that the php
program accepts.
如果您运行,php --help
您可以看到php
程序接受的命令列表。
-a Run as interactive shell
-c <path>|<file> Look for php.ini file in this directory
-n No php.ini file will be used
-d foo[=bar] Define INI entry foo with value 'bar'
-e Generate extended information for debugger/profiler
-f <file> Parse and execute <file>.
-h This help
-i PHP information
-l Syntax check only (lint)
-m Show compiled in modules
-r <code> Run PHP <code> without using script tags <?..?>
-B <begin_code> Run PHP <begin_code> before processing input lines
-R <code> Run PHP <code> for every input line
-F <file> Parse and execute <file> for every input line
-E <end_code> Run PHP <end_code> after processing all input lines
-H Hide any passed arguments from external tools.
-S <addr>:<port> Run with built-in web server.
-t <docroot> Specify document root <docroot> for built-in web server.
-s Output HTML syntax highlighted source.
-v Version number
-w Output source with stripped comments and whitespace.
-z <file> Load Zend extension <file>.
args... Arguments passed to script. Use -- args when first argument
starts with - or script is read from stdin
--ini Show configuration file names
--rf <name> Show information about function <name>.
--rc <name> Show information about class <name>.
--re <name> Show information about extension <name>.
--rz <name> Show information about Zend extension <name>.
--ri <name> Show configuration for extension <name>.