如何从命令行执行PHP代码? [英] How can I execute PHP code from the command line?

查看:47
本文介绍了如何从命令行执行PHP代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想直接在命令行执行类似 if(function_exists("my_func")))echo'function exist'; 的单个PHP语句,而不必使用单独的PHP文件.

I would like to execute a single PHP statement like if(function_exists("my_func")) echo 'function exists'; directly with the command line without having to use a separate PHP file.

怎么可能?

推荐答案

如果要在命令行中使用PHP,建议您安装

If you're going to do PHP in the command line, I recommend you install phpsh, a decent PHP shell. It's a lot more fun.

无论如何, php 命令提供了两个用于从命令行执行代码的开关:

Anyway, the php command offers two switches to execute code from the command line:

-r <code>        Run PHP <code> without using script tags <?..?>
-R <code>        Run PHP <code> for every input line

您可以这样使用 php -r开关:

php -r 'echo function_exists("foo") ? "yes" : "no";'

上面的PHP命令应该输出 ,并返回 0 ,如您所见:

The above PHP command above should output no and returns 0 as you can see:

>>> php -r 'echo function_exists("foo") ? "yes" : "no";'
no
>>> echo $? # print the return value of the previous command
0

另一个有趣的开关是 php -a :

-a               Run as interactive shell

phpsh 相比,这很<脚,但如果您不这样做,想要安装 Facebook制作的PHP很棒的交互式shell,以获取制表符的完成情况,历史记录等,然后就这样使用-a :

>>> php -a
Interactive shell

php > echo function_exists("foo") ? "yes" : "no";
no
php >

如果它在您的盒子上不起作用,例如在我的盒子上(在测试en.wikipedia.org/wiki/Ubuntu_%28operating_system%29"rel =" nofollow noreferrer> Ubuntu 和

If it doesn't work on your box like on my boxes (tested on Ubuntu and Arch Linux), then probably your PHP setup is fuzzy or broken. If you run this command:

php -i | grep 'API'

应该请参见:

Server API => Command Line Interface

否则,这意味着也许另一个命令将提供CLI SAPI .尝试php-cli;也许是操作系统中可用的软件包或命令.

If you don't, this means that maybe another command will provides the CLI SAPI. Try php-cli; maybe it's a package or a command available in your OS.

如果您这样做,则看到您的 php 命令使用CLI(命令行界面)SAPI(服务器API),然后运行 php -h |.grep code 找出哪个疯狂的选择-因为这一年没有改变-允许在您的版本/设置中运行代码.

If you do see that your php command uses the CLI (command-line interface) SAPI (Server API), then run php -h | grep code to find out which crazy switch - as this hasn't changed for year- allows to run code in your version/setup.

另外两个例子,只是为了确保它能在我的盒子上正常工作:

Another couple of examples, just to make sure it works on my boxes:

>>> php -r 'echo function_exists("sg_load") ? "yes" : "no";'
no
>>> php -r 'echo function_exists("print_r") ? "yes" : "no";'
yes

此外,请注意,有可能在CLI中而不是CGI或Apache SAPI中加载了扩展.几个PHP SAPI可能使用不同的php.ini文件,例如/etc/php/cli/php.ini /etc/php/cgi/php.ini /etc/php/apache/php.ini Gentoo Linux 框.找出哪个ini文件与 php -i |一起使用.grep ini .

Also, note that it is possible that an extension is loaded in the CLI and not in the CGI or Apache SAPI. It is likely that several PHP SAPIs use different php.ini files, e.g., /etc/php/cli/php.ini vs. /etc/php/cgi/php.ini vs. /etc/php/apache/php.ini on a Gentoo Linux box. Find out which ini file is used with php -i | grep ini.

这篇关于如何从命令行执行PHP代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆