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.
我想执行一个php语句,比如if(function_exist(“my_func”))echo 'function exists';直接使用命令行而不必使用分离的php文件。
How is it possible ?
怎么可能呢?
2 个解决方案
#1
186
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 switch as such:
可以使用php的-r开关:
php -r 'echo function_exists("foo") ? "yes" : "no";'
The above PHP command above should output no
and returns 0
as 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来获得tab补全,历史等等,那么就用a吧:
>>> 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:
如果它不能像我的box*es*(在Ubuntu和Arch上测试)那样在您的box上工作,那么您的PHP设置可能是模糊的或损坏的。如果您运行以下命令:
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.
如果没有,这意味着可能会有另一个命令提供CLI SAPI。试试php-cli,也许它是一个包或一个命令在您的操作系统中可用。
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.
如果您看到您的php命令使用CLI(命令行接口)SAPI(服务器API),那么运行php -h | grep代码来找出哪个疯狂的开关——因为这一年没有更改——允许在您的版本/设置中运行代码。
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
.
另外,请注意,扩展可能是在CLI中加载的,而不是在CGI或Apache SAPI中。几个PHP sapi可能使用不同的PHP。ini文件,例如/etc/php/cli/php.ini vs /etc/php/cgi/php.ini vs /etc/php/apache/php.我在Gentoo盒子上。找出与php -i - | grep ini一起使用的ini文件。
#2
1
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.
如果它在那里,你会得到一些文本。如果没有,你什么也得不到。
#1
186
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 switch as such:
可以使用php的-r开关:
php -r 'echo function_exists("foo") ? "yes" : "no";'
The above PHP command above should output no
and returns 0
as 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来获得tab补全,历史等等,那么就用a吧:
>>> 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:
如果它不能像我的box*es*(在Ubuntu和Arch上测试)那样在您的box上工作,那么您的PHP设置可能是模糊的或损坏的。如果您运行以下命令:
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.
如果没有,这意味着可能会有另一个命令提供CLI SAPI。试试php-cli,也许它是一个包或一个命令在您的操作系统中可用。
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.
如果您看到您的php命令使用CLI(命令行接口)SAPI(服务器API),那么运行php -h | grep代码来找出哪个疯狂的开关——因为这一年没有更改——允许在您的版本/设置中运行代码。
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
.
另外,请注意,扩展可能是在CLI中加载的,而不是在CGI或Apache SAPI中。几个PHP sapi可能使用不同的PHP。ini文件,例如/etc/php/cli/php.ini vs /etc/php/cgi/php.ini vs /etc/php/apache/php.我在Gentoo盒子上。找出与php -i - | grep ini一起使用的ini文件。
#2
1
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.
如果它在那里,你会得到一些文本。如果没有,你什么也得不到。