在命令行上执行一串PHP代码 [英] execute a string of PHP code on the command line
问题描述
我想在命令行上运行一行PHP代码,类似于以下选项的工作原理:
I'd like to be able to run a line of PHP code on the command line similar to how the following options work:
:~> perl -e "print 'hi';"
:~> python -c "print 'hi'"
:~> ruby -e "puts 'hi'"
我想要能够: p>
I'd like to be able to do:
:~> php "echo 'hi';"
我读过,有一个-r选项可以做我需要的php,但是它似乎不可用,当我尝试使用它。我试过使用PHP 5.2.13和PHP 4.4.9,也没有-r选项。
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.
我写了这个脚本(我叫run_php.php) - 工作,但我不是一个巨大的粉丝,只是因为我觉得应该有一个更正确的方式来做。
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.
#!/usr/bin/php5 -q
<?php echo eval($argv[1]); ?>
我的问题是:是否有-r选项?如果是这样,为什么当我运行--help它不可用?如果没有-r选项,什么是最好的方法(如果可能,不编写中间脚本)?
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)?
谢谢!
=== EDIT ===
=== EDIT ===
因为我不认为上面很清楚,所以-r选项不可用我。这是我运行的两个版本的PHP的php -h输出。
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.
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>.
没有-r选项。当我尝试使用-r选项时,我得到:
There is NO -r option. When I try to use the -r option I get:
Error in argument 1, char 2: option not found r
对于混淆而感到抱歉。
推荐答案
EDIT2:是的,它在 PHP 5.2's cli SAPI 。
Yep, it's there in PHP 5.2's cli SAPI.
编辑:如果您无法升级在PHP 5.2中没有这样的选项(我没有手头测试)的情况下,你可以这样做:
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:
glopes@nebm:~$ echo "<?php echo \"hi\\n\";" | php
hi
原文:
code> -r 选项(虽然我不确定PHP 5.2):
There is indeed a -r
option (though I'm not sure about PHP 5.2):
D:\>php -r "echo 'hi';";
hi
只需确保您使用的是命令行版本的PHP。 php --version
应该给你这样的东西(注意cli):
Just make sure you are using the command line version of PHP. php --version
should give you something like this (notice "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
这篇关于在命令行上执行一串PHP代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!