php 在命令行执行一串PHP代码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/2954540/
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
execute a string of PHP code on the command line
提问by Matthew J Morrison
I'd like to be able to run a line of PHP code on the command line similar to how the following options work:
我希望能够在命令行上运行一行 PHP 代码,类似于以下选项的工作方式:
:~> perl -e "print 'hi';"
:~> python -c "print 'hi'"
:~> ruby -e "puts 'hi'"
I'd like to be able to do:
我希望能够做到:
:~> php "echo 'hi';"
I've read that there is a -r option that can do what I need for php, however it doesn't appear to be available when I try to use it. I've tried using PHP 5.2.13 and PHP 4.4.9 and neither have an -r option available.
我读过有一个 -r 选项可以完成我对 php 的需求,但是当我尝试使用它时它似乎不可用。我试过使用 PHP 5.2.13 和 PHP 4.4.9,但都没有 -r 选项可用。
I wrote this script (that I called run_php.php) - which works, but I'm not a huge fan of it just because I feel like there should be a more "correct" way to do it.
我写了这个脚本(我称之为 run_php.php) - 它有效,但我不是它的忠实粉丝,因为我觉得应该有一个更“正确”的方法来做到这一点。
#!/usr/bin/php5 -q
<?php echo eval($argv[1]); ?> 
My question is: is there a -r option? If so, why is it not available when I run --help? If there is no -r option, what is the best way to do this (without writing an intermediary script if possible)?
我的问题是:有 -r 选项吗?如果是这样,为什么在我运行 --help 时它不可用?如果没有 -r 选项,最好的方法是什么(如果可能,不编写中间脚本)?
Thanks!
谢谢!
=== EDIT ===
=== 编辑 ===
Because I don't think it was very clear above, the -r option is NOT available to me. Here is the php -h output for both versions of PHP that I'm running.
因为我认为上面不是很清楚,所以 -r 选项对我不可用。这是我正在运行的两个 PHP 版本的 php -h 输出。
PHP 4.4.9
PHP 4.4.9
Usage: php [-q] [-h] [-s] [-v] [-i] [-f <file>] 
       php <file> [args...]
  -a               Run interactively
  -C               Do not chdir to the script's directory
  -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 <file>.  Implies `-q'
  -h               This help
  -i               PHP information
  -l               Syntax check only (lint)
  -m               Show compiled in modules
  -q               Quiet-mode.  Suppress HTTP Header output.
  -s               Display colour syntax highlighted source.
  -v               Version number
  -w               Display source with stripped comments and whitespace.
  -z <file>        Load Zend extension <file>.
php 5.2.13
php 5.2.13
Usage: php [-q] [-h] [-s] [-v] [-i] [-f <file>]
       php <file> [args...]
  -a               Run interactively
  -C               Do not chdir to the script's directory
  -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 <file>.  Implies `-q'
  -h               This help
  -i               PHP information
  -l               Syntax check only (lint)
  -m               Show compiled in modules
  -q               Quiet-mode.  Suppress HTTP Header output.
  -s               Display colour syntax highlighted source.
  -v               Version number
  -w               Display source with stripped comments and whitespace.
  -z <file>        Load Zend extension <file>.
There is NO -r option. When I try to use the -r option I get:
没有 -r 选项。当我尝试使用 -r 选项时,我得到:
Error in argument 1, char 2: option not found r
Sorry for the confusion.
对困惑感到抱歉。
回答by Artefacto
EDIT2: Yep, it's there in PHP 5.2's cli SAPI.
EDIT2:是的,它在PHP 5.2 的 cli SAPI 中。
EDIT: If you cannot upgrade and that's the case that there's no such option in PHP 5.2 (I don't have it at hand to test), you can do this:
编辑:如果您无法升级并且在 PHP 5.2 中没有这样的选项(我手头没有它来测试),您可以这样做:
glopes@nebm:~$ echo "<?php echo \"hi\n\";" | php hi
Original:
原来的:
There is indeed a -roption (though I'm not sure about PHP 5.2):
确实有一个-r选项(虽然我不确定 PHP 5.2):
D:\>php -r "echo 'hi';"; hi
Just make sure you are using the command line version of PHP. php --versionshould give you something like this (notice "cli"):
只要确保您使用的是命令行版本的 PHP。php --version应该给你这样的东西(注意“cli”):
D:\>php --version PHP 5.3.0 (cli) (built: May 20 2010 19:05:12) (DEBUG) Copyright (c) 1997-2009 The PHP Group Zend Engine v2.3.0, Copyright (c) 1998-2009 Zend Technologies
回答by Damian W.
In new versions of PHP you just type "php -a" and you hop into an interactive mode, where you can experiment with PHP.
在新版本的 PHP 中,您只需键入“php -a”并跳转到交互模式,您可以在其中试验 PHP。
回答by Sumoanand
Extra Semi colon is not required in the end.
最后不需要额外的分号。
You can mention php -r "echo 'hi';"instead of php -r "echo 'hi';";
你可以提到php -r "echo 'hi';"而不是php -r "echo 'hi';";
Another example(To get current timestamp at command line):
另一个例子(在命令行获取当前时间戳):
php -r 'print time()."\n";'
回答by Benubird
The easiest way to do it is to use the -r flag. However, I've found that it does not allow multiline inputs. To work around that, you can do this:
最简单的方法是使用 -r 标志。但是,我发现它不允许多行输入。要解决此问题,您可以执行以下操作:
php -r "passthru(file_get_contents('php://stdin'));"
Which lets you pipe from stdin, like so:
它可以让你从标准输入管道,像这样:
echo -n "echo 'test';" | php -r "passthru(file_get_contents('php://stdin'));"
However, to answer the original question, if you do not have the -r flag available, it is also possible to use the -f flag - just pass stdin as the file to open: php -f /dev/stdin
但是,要回答原始问题,如果您没有可用的 -r 标志,也可以使用 -f 标志 - 只需将 stdin 作为要打开的文件传递: php -f /dev/stdin
If you do this, be aware that a) you need to include a blank space at the start of the input, and b) you have to open with <?php. Example:
如果您这样做,请注意 a) 您需要在输入的开头包含一个空格,并且 b) 您必须以<?php. 例子:
echo -ne " <?php\necho 'test';" | php -f /dev/stdin
回答by allnightgrocery
Take a look at this page on PHP command line features, if you haven't already. There are some posts on issues based on OS and double or single quotes.
如果您还没有,请查看有关PHP 命令行功能的页面。有一些关于基于操作系统和双引号或单引号的问题的帖子。
I would also check the PHP information
我也会检查PHP信息
php -i
php -i
to see if PHP was compiled with CLI support disabled(--disable-cli).
查看 PHP 是否是在禁用 CLI 支持的情况下编译的(--disable-cli)。

