我希望能够在命令行上运行一行PHP代码,类似于以下选项的工作方式:
:~> perl -e "print 'hi';"
:~> python -c "print 'hi'"
:~> ruby -e "puts 'hi'"
Run Code Online (Sandbox Code Playgroud)
我希望能够做到:
:~> php "echo 'hi';"
Run Code Online (Sandbox Code Playgroud)
我已经读过有一个-r选项可以做我需要的php,但是当我尝试使用它时它似乎不可用.我尝试过使用PHP 5.2.13和PHP 4.4.9,并且都没有-r选项.
我写了这个脚本(我称之为run_php.php) - 这是有效的,但我并不是它的忠实粉丝,因为我觉得应该有更"正确"的方法来做到这一点.
#!/usr/bin/php5 -q
<?php echo eval($argv[1]); ?>
Run Code Online (Sandbox Code Playgroud)
我的问题是:是否有-r选项?如果是这样,为什么我运行时无法使用--help?如果没有-r选项,那么执行此操作的最佳方法是什么(如果可能,不编写中间脚本)?
谢谢!
===编辑===
因为我认为上面不是很清楚,所以-r选项不适用于我.这是我正在运行的两个版本的PHP的php -h输出.
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>.
Run Code Online (Sandbox Code Playgroud)
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>.
Run Code Online (Sandbox Code Playgroud)
没有-r选项.当我尝试使用-r选项时,我得到:
Error in argument 1, char 2: option not found r
Run Code Online (Sandbox Code Playgroud)
对困惑感到抱歉.
Art*_*cto 91
编辑2:是的,它在PHP 5.2的cli SAPI中.
编辑:如果你无法升级,那就是PHP 5.2中没有这样的选项(我手边没有测试),你可以这样做:
glopes@nebm:~$ echo "<?php echo \"hi\\n\";" | php hi
原版的:
确实有一个-r选项(虽然我不确定PHP 5.2):
D:\>php -r "echo 'hi';"; hi
只需确保使用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
最简单的方法是使用标志-r。但是,我发现它不允许多行输入。要解决这个问题,您可以这样做:
php -r "passthru(file_get_contents('php://stdin'));"
Run Code Online (Sandbox Code Playgroud)
它允许您从标准输入进行管道传输,如下所示:
echo -n "echo 'test';" | php -r "passthru(file_get_contents('php://stdin'));"
Run Code Online (Sandbox Code Playgroud)
但是,要回答最初的问题,如果没有可用-r的标志,也可以使用 -f 标志 - 只需传递标准输入作为要打开的文件:php -f /dev/stdin
如果您这样做,请注意 a) 您需要在输入的开头包含一个空格,b) 您必须以<?php. 例子:
echo -ne " <?php\necho 'test';" | php -f /dev/stdin
Run Code Online (Sandbox Code Playgroud)
最后不需要多余的半冒号。
您可以提及php -r "echo 'hi';"而不是php -r "echo 'hi';";
另一个示例(要在命令行中获取当前时间戳):
php -r 'print time()."\n";'
Run Code Online (Sandbox Code Playgroud)