Linux 如何从命令行执行 PHP 代码?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9520090/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 04:56:15  来源:igfitidea点击:

How to execute PHP code from the command line?

phplinuxfunctionshellcommand-line

提问by Steve

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.

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

How is it possible ?

这怎么可能 ?

采纳答案by jpic

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,我建议您安装phpsh,一个不错的 PHP shell这更有趣。

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

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

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

You can use php's -r switchas such:

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

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

The above PHP command above should outputnoand returns0as you can see:

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

>>> 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:

另一个有趣的开关是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:

phpsh相比,有点蹩脚,但是如果您不想安装由 facebook 制作的很棒的 php 交互式 shell 来获取选项卡完成、历史记录等,那么请使用 -a

>>> php -a
Interactive shell

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

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

如果它不能像我的 box* es*(在 Ubuntu 和 Arch 上测试)那样在你的盒子上工作,那么你的 PHP 设置可能是模糊的或损坏的。如果您运行此命令:

php -i | grep 'API'

You shouldsee:

应该看到:

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.

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

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

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

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.inivs /etc/php/cgi/php.inivs /etc/php/apache/php.inion a Gentoo box. Find out which ini file is used with php -i | grep ini.

另请注意,扩展可能加载到 CLI 而不是 CGI 或 Apache SAPI 中。很可能有几个 PHP SAPI 使用不同的 php.ini 文件,例如在 Gentoo 机器上的/etc/php/cli/php.inivs /etc/php/cgi/php.inivs。/etc/php/apache/php.ini找出哪个 ini 文件与php -i | grep ini.

回答by Matt Gibson

On the command line:

在命令行上:

php -i | grep sourceguardian

If it's there, then you'll get some text. If not, you won't get a thing.

如果它在那里,那么你会得到一些文本。如果没有,你就什么也得不到。

回答by fred727

You can use :

您可以使用 :

 echo '<?php if(function_exists("my_func")) echo "function exists"; ' | php

The short tag "< ?=" can be helpfull too :

短标签“ <?=”也很有帮助:

 echo '<?= function_exists("foo") ? "yes" : "no";' | php
 echo '<?= 8+7+9 ;' | php

Tha closing tag "?>" is optional, but don't forget the final ";" !

结束标记“?>”是可选的,但不要忘记最后的“;” !

回答by user1942505

Using PHP from the command line

从命令行使用 PHP

use "instead of 'on windows when using the cli version with -r

使用带有 -r 的 cli 版本时,在 Windows 上使用"而不是'

php -r "echo 1"

-- correct

- 正确的

php -r 'echo 1'

-- incorrect

-- 不正确

  PHP Parse error:  syntax error, unexpected ''echo' (T_ENCAPSED_AND_WHITESPACE), expecting end of file in Command line code on line 1