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

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

问题描述

我想直接使用命令行执行一个像 if(function_exists("my_func")) echo 'function exists'; 这样的 PHP 语句,而不必使用单独的 PHP 文件.

怎么可能?

解决方案

如果您打算在命令行中执行 PHP,我建议您安装 phpsh,一个不错的 PHP shell.它更有趣.

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

-r 运行 PHP <代码>不使用脚本标签 <?..?>-R<代码>运行 PHP <代码>对于每条输入线

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

php -r 'echo function_exists("foo") ?是":不";'

上面的 PHP 命令应该 output noreturns 0 如您所见:

<预><代码>>>>php -r 'echo function_exists("foo") ?是":不";'不>>>回声 $?# 打印上一条命令的返回值0

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

-a 作为交互式 shell 运行

phpsh相比,它有点蹩脚,但如果你不这样做想要安装 由 Facebook 制作的很棒的 PHP 交互式 shell 以获取选项卡完成、历史记录等,然后像这样使用-a:

<预><代码>>>>php -a交互式外壳php >echo function_exists(foo") ?是":不";不php >

如果它在你的盒子上不起作用就像在我的盒子上一样es(UbuntuArch Linux),那么可能是您的 PHP 设置模糊或损坏.如果你运行这个命令:

php -i |grep'API'

应该看到:

服务器 API =>命令行界面

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

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

另外几个例子,只是为了确保它适用于我的盒子:

<预><代码>>>>php -r 'echo function_exists("sg_load") ?是":不";'不>>>php -r 'echo function_exists("print_r") ?是":不";'是的

另外,请注意扩展程序可能加载到 CLI 而不是 CGI 或 Apache SAPI.很可能几个 PHP SAPI 使用不同的 php.ini 文件,例如 /etc/php/cli/php.ini/etc/php/cgi/php.ini/etc/php/apache/php.iniGentoo Linux 框.找出哪个 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 separate 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 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'

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

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

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