問題描述
我想直接使用命令行執行一個像 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 no
和 returns 0
如您所見:
另一個有趣的開關是php -a:
-a 作為交互式 shell 運行
與phpsh相比,它有點蹩腳,但如果你不這樣做想要安裝 由 Facebook 制作的很棒的 PHP 交互式 shell 以獲取選項卡完成、歷史記錄等,然后像這樣使用-a:
<預><代碼>>>>php -a交互式外殼php >echo function_exists(foo") ?是":不";不php >如果它在你的盒子上不起作用就像在我的盒子上一樣es(在 Ubuntu 和 Arch 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.ini
在 Gentoo 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 代碼?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!