如何从终端执行php块而不保存到文件

Ale*_*des 26 php terminal command

假设我有一个代码块,我想像这样测试:

<?php 
 Print "Hello, World!";
?>
Run Code Online (Sandbox Code Playgroud)

如何从终端快速运行此代码而不将其保存到文件中?

我试过像......

php -r "Print "Hello, World!";"
Run Code Online (Sandbox Code Playgroud)

但只是抱怨语法错误.必须有一种简单的方法来做到这一点.我还没有找到任何解释.

Meh*_*ini 45

在第一次安装PHP时快速访问终端中的PHP,然后运行:

php -a
Run Code Online (Sandbox Code Playgroud)

细节:

在此输入图像描述

php -a打开一个交互式shell,用于直接键入php命令并立即查看结果,例如php -a在linux shell中键入echo 'Hello World';后,您可以键入,然后按Enter键Hello World!将打印在屏幕上.

Windows解决方案

在Windows中没有与Linux相同的交互模式导致Windows无法从命令行读取行,但仍然可以使用交互式模式!所以在Windows上打开php安装它的地方,例如,如果你使用xampp php打开C:\xampp\php并且然后键入php -a您在终端中键入的内容,但在每个部分的末尾,您只想按Ctrl+Z,然后按Enter 键查看结果.

php -a
echo 'hello world!';
^Z
Run Code Online (Sandbox Code Playgroud)


ale*_*lex 41

转义"用于分隔字符串的内部双引号().

php -r "Print \"Hello, World!\";"
Run Code Online (Sandbox Code Playgroud)

或者,'对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>.
Run Code Online (Sandbox Code Playgroud)