[英]How to execute php block from terminal without saving to a file
假设我有一个代码块,我想像这样测试:
<?php
Print "Hello, World!";
?>
如何从终端快速运行此代码而不将其保存到文件?
我尝试了诸如...
php -r "Print "Hello, World!";"
但刚刚收到有关语法错误的投诉。 必须有一个简单的方法来做到这一点。 我只是还没有找到任何解释。
要立即在终端上运行 PHP 命令,您可以将-a
选项传递给已安装的 PHP:
php -a
详情:
php -a
打开一个交互式 shell,让您直接输入 PHP 命令并在终端上查看结果,例如,在终端中输入php -a
,您可以输入echo 'Hello World';
然后按 Enter Hello World!
将打印在屏幕上。
视窗解决方案
在 Windows 上,没有与 Linux 相同的交互模式,但您仍然可以使用类似交互模式!,因此,在安装它的地方打开 PHP,例如,如果您使用 XAMPP,那么您的 PHP 应该位于C:\\xampp\\php
(或将二进制目录添加到环境变量中),然后键入php -a
,在每行的末尾,您可以通过按Ctrl+Z
和Enter
来查看结果。
php -a
echo 'hello world!';
^Z
转义用于分隔字符串的内部双引号 ( "
)。
php -r "Print \"Hello, World!\";"
或者,对 PHP 字符串或对 PHP 代码的引用使用单引号 ( '
)。
如果您运行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>.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.