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

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

问题描述

我想直接使用命令行执行单个php语句,如 if(function_exists(my_func))echo'function exists'; 一个单独的php文件。



如何实现?

解决方案

如果您要在命令行中使用PHP,我建议您安装 phpsh,一个体面的PHP shell

无论如何,php命令提供两个开关从命令行执行代码

  -r< code>运行PHP< code>而不使用脚本标签<?..?> 
-R< code>运行PHP< code>对于每个输入行

您可以使用php的 -r开关

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

上面的PHP命令应该 no 返回 0 ,如下所示:

 >>> php -r'echo function_exists(foo)? yes:no;'
no
>>>> echo $? #打印上一个命令的返回值
0

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

  -a作为交互式shell运行

phpsh 相比, strong>,但如果您不想安装由facebook制作的php的真棒交互式shell以获取标签完成,历史记录等, a>,然后使用-a as



 > php -a 
交互式shell

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

如果不起作用 在Ubuntu和Arch上测试可能是您的PHP设置模糊或破碎。如果您运行此命令:

  php -i | grep'API'

查看:

 服务器API =>命令行界面

如果不,这意味着也许另一个命令将提供CLI SAPI 。尝试php-cli,也许它是一个包或在您的操作系统中可用的命令。



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



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

 > > 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 vs /etc/php/cgi/php.ini vs /etc/php/apache/php.ini 。找出哪个ini文件与 php -i |配合使用grep ini


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 seperate php file.

How is it possible ?

解决方案

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.

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

You can use php's -r switch as such:

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

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

Another funny switch is php -a:

-a               Run as interactive shell

It's sort of lame compared to phpsh, but if you don't want to install the awesome interactive shell for php made by facebook to get tab completion, history, and so on, then use -a as such:

>>> php -a
Interactive shell

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

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

php -i | grep 'API'

You should see:

Server API => Command Line Interface

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.

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

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 box. Find out which ini file is used with php -i | grep ini.

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

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