您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

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

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

这很有趣。

无论如何,PHP命令提供了 :

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

您可以这样使用PHP的 :

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

上面的PHP命令应该 no0如您所见:

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

一个有趣的开关是 :

-a               Run as interactive shell

这 ,但是如果您不想安装facebook制作的php真棒交互式shell来获取制表符的完成情况,历史记录等等,则可以 :

>>> PHP -a
Interactive shell

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

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

PHP -i | grep 'API'

您 看到:

Server API => Command Line Interface

,这意味着 。尝试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中加载了扩展。 ,例如/etc/PHP/cli/PHP.inivs /etc/PHP/cgi/PHP.ini与vs/etc/PHP/apache/PHP.ini在Gentoo盒子上。找出与哪个ini文件一起使用PHP -i | grep ini

php 2022/1/1 18:18:55 有312人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶