运算符
在线手册:中文  英文

执行运算符

PHP 支持一个执行运算符:反引号(``)。注意这不是单引号!PHP 将尝试将反引号中的内容作为外壳命令来执行,并将其输出信息返回(即,可以赋给一个变量而不是简单地丢弃到标准输出)。使用反引号运算符“`”的效果与函数 shell_exec() 相同。

<?php
$output 
= `ls -al`;
echo 
"<pre>$output</pre>";
?>

Note:

反引号运算符在激活了安全模式或者关闭了 shell_exec() 时是无效的。

Note:

与其它某些语言不同,反引号不能在双引号字符串中使用。

参见手册中程序执行函数popen()proc_open() 以及 PHP 的命令行模式


运算符
在线手册:中文  英文

用户评论:

Anonymous (2013-03-21 23:11:40)

escapeshellarg()
if you're usimg popen, `` exec or anything else which takes a command-line
use escapeshellarg() on any variable parts, or risk being pwned.

inform dot to dot kannan at gmail dot com (2012-08-30 16:21:12)

You can use the backtick operator on windows to run windows command too and it is not restricted only to execute the exe. 
Example:
<?php
$output 
= `dir`;
echo 
"<pre>$output</pre>";
?>
It lists all the files in the directory

joseph dot bruni at gmail dot com (2011-01-12 15:41:12)

If you have problems with the buffer size for the return result, use the popen() command instead. This will allow you to do file I/O on the result without any buffer limits.

robert (2006-03-01 11:25:41)

Just a general usage note. I had a very difficult time solving a problem with my script, when I accidentally put one of these backticks at the beginning of a line, like so:
[lots of code]
` $URL = "blah...";
[more code]
Since the backtick is right above the tab key, I probably just fat-fingered it while indenting the code.
What made this so hard to find, was that PHP reported a parse error about 50 or so lines *below* the line containing the backtick. (There were no other backticks anywhere in my code.) And the error message was rather cryptic:
Parse error: parse error, expecting `T_STRING' or `T_VARIABLE' or `T_NUM_STRING' in /blah.php on line 446
Just something to file away in case you're pulling your hair out trying to find an error that "isn't there."

cs at kainaw dot com (2005-12-29 14:55:21)

After much trouble, I have concluded that the backtick operator (and shell_exec) have a limited buffer for the return. My problem was that I was grepping a file with over 500,000 lines, receiving a response with well over 100,000 lines. After a short pause, I was flooded with errors from grep about the pipe being closed.
I have searched, but I cannot find the exact size of the buffer used by the backtick operator and shell_exec. So, to avoid this error, you must limit the output of your commands (such as using -m with grep). Through trial and error, you can get the command to run without error.

shakil dot tanvir at gmail dot com (2005-12-22 05:25:42)

For passing parameter to a executable doesn't need an executable. Also it may create problem specifically for CGI Bin aplicatioin. Have a look at the following code:

<?php

$parFile
="param.txt";
$parImage="mohona.gif";

$output=`C:\ms4w\Apache\cgi-bin\owtchart.exe $parFile $parImage`;
echo 
"<pre>$output</pre>";

?>

In the above code "owtchart.exe" takes two parameters. One is a text file(param.txt) and another is a name of a GIF file where output will be created. It works fine and doesn't need any BAT file!

vdboor at codingdomain dot com (2005-11-23 00:10:23)

Note that most OS-es define two channels for file-output, the stdout and stderr (standard out and standard error). To read the data sent to stderr too, include 2>&1 in the backticks.

aaron dot bentley at utoronto dot ca (2003-07-20 16:45:20)

waylanator's example can be dangerous, since it doesn't prevent characters with special meaning from being emitted to the commandline. Programming errors or untrusted data could cause serious problems. At the bare minimum, remove all non-alphanumeric characters before passing a string to the shell. escapeshellarg() is also useful in *nix environments, but usually the best approach is to bypass the shell, using exec() etc.

thierry_bo at php dot net (2003-05-23 10:45:31)

dexter at ragehosting dot net (2003-03-27 23:18:29)

Or just %* (i think) to pass ALL variables specified

waylanator no at spam hotmail dot com (2003-02-14 12:03:16)

Want to pass a parameter with your batch file to the executable?
Just do this:

mybat.bat:
__________
@echo off
c:\progra~1\myprog~1\program.exe %1
__________

php:
__________
<?php
$par
"my_parameter";
$test=`c:\mybat.bat $par`;
echo 
"<pre>$test</pre>";
?>
__________

Have more than 1 parameter?  Just add %2 %3 %4 and so on in the batch file.

Hope this helps someone.

waylanator no at spam hotmail dot com (2003-02-14 09:12:54)

In Windows it appears you can only call an executable file that resides in the system path which is defined by Windows.  As a workaround you can place a batch file in the system path that calls the program from it's dir. Just make sure to use short MS-DOS file and dir names.
For example:
If you were calling the file c:\program files\my program\program.exe do this:

mybat.bat look like this:
_________
@echo off
c:\progra~1\myprog~1\program.exe
_________

Save mybat.bat in c:\ or c:\windows or any other dir in the system path as defined by windows.

Then in php call the batch file:
_________
<?php
$test 
= `c:\mybat.bat`;
echo 
"<pre>$test</pre>";
?>
_________

That should do it.
Of course this will only work for a program you can run from the MS-DOS command prompt, but (as I understant it) that goes for any executable you call with PHP anyway.
Tested in Win98 running Apache 1.3.27 and PHP 4.3.0

reed-NO at SPAM-zerohour dot net (2002-03-13 00:45:32)

When a program is run using backticks, and the user cancels page loading (if your program is taking too long!), the shell running the program (the one in the backticks) may continue indefinitely on the server. I do not know if this is a bug, or just a danger of using this feature. (It may depend on the way the browser "cancels" the request -- it was a problem on both IE and OmniWeb for the Mac). Beware!

易百教程