本文翻译自:PHP exec() vs system() vs passthru()
What are the differences? 有什么区别?
Is there a specific situation or reason for each function? 每个功能都有特定的情况或原因吗? If yes, can you give some examples of those situations? 如果是的话,你能举一些这些情况的例子吗?
PHP.net says that they are used to execute external programs. PHP.net说它们用于执行外部程序。 see reference From the examples I see, I don't see any obvious difference. 参见参考从我看到的例子中,我没有看到任何明显的区别。
If I were to simply run a script (bash or python), which function do you recommend me to use? 如果我只是简单地运行一个脚本(bash或python),你建议我使用哪个函数?
#1楼
参考:https://stackoom.com/question/34ds/PHP-exec-vs-system-vs-passthru
#2楼
As drawn from http://php.net/ && Chipmunkninja : 从绘制http://php.net/ && Chipmunkninja :
The system() Function system()函数
The system function in PHP takes a string argument with the command to execute as well as any arguments you wish passed to that command. PHP中的系统函数接受一个带有要执行的命令的字符串参数以及您希望传递给该命令的任何参数。 This function executes the specified command, and dumps any resulting text to the output stream (either the HTTP output in a web server situation, or the console if you are running PHP as a command line tool). 此函数执行指定的命令,并将任何结果文本转储到输出流(Web服务器情况下的HTTP输出,或者如果您将PHP作为命令行工具运行,则控制台)。 The return of this function is the last line of output from the program, if it emits text output. 如果它发出文本输出,则返回此函数是程序的最后一行输出。
The system function is quite useful and powerful, but one of the biggest problems with it is that all resulting text from the program goes directly to the output stream. 系统功能非常有用且功能强大,但其中一个最大的问题是程序中的所有结果文本都直接转到输出流。 There will be situations where you might like to format the resulting text and display it in some different way, or not display it at all. 在某些情况下,您可能希望格式化生成的文本并以不同的方式显示它,或者根本不显示它。
For this, the exec function in PHP is perfectly adapted. 为此,PHP中的exec函数完全适应。 Instead of automatically dumping all text generated by the program being executed to the output stream, it gives you the opportunity to put this text in an array returned in the second parameter to the function: 它不是将正在执行的程序生成的所有文本自动转储到输出流,而是让您有机会将此文本放在第二个参数返回的数组中:
The shell_exec() Function shell_exec()函数
Most of the programs we have been executing thus far have been, more or less, real programs1. 到目前为止,我们执行的大多数程序或多或少都是真正的程序1。 However, the environment in which Windows and Unix users operate is actually much richer than this. 但是,Windows和Unix用户运行的环境实际上比这更丰富。 Windows users have the option of using the Windows Command Prompt program, cmd.exe This program is known as a command shell. Windows用户可以选择使用Windows命令提示符程序cmd.exe此程序称为命令shell。
The passthru() Function passthru()函数
One fascinating function that PHP provides similar to those we have seen so far is the passthru function. PHP提供的一个令人着迷的功能类似于我们迄今为止看到的功能是passthru功能。 This function, like the others, executes the program you tell it to. 与其他函数一样,此函数执行您告诉它的程序。 However, it then proceeds to immediately send the raw output from this program to the output stream with which PHP is currently working (ie either HTTP in a web server scenario, or the shell in a command line version of PHP). 但是,它会立即将此程序的原始输出发送到PHP当前正在使用的输出流(即Web服务器方案中的HTTP,或PHP命令行版本中的shell)。
The proc_open() Function and popen() function proc_open()函数和popen()函数
proc_open() is similar to popen() but provides a much greater degree of control over the program execution. proc_open()类似于popen(),但提供了对程序执行的更大程度的控制。 cmd is the command to be executed by the shell. cmd是shell要执行的命令。 descriptorspec is an indexed array where the key represents the descriptor number and the value represents how PHP will pass that descriptor to the child process. descriptorspec是一个索引数组,其中键表示描述符编号,值表示PHP将该描述符传递给子进程的方式。 pipes will be set to an indexed array of file pointers that correspond to PHP's end of any pipes that are created. 管道将被设置为文件指针的索引数组,这些数组对应于PHP创建的任何管道的末尾。 The return value is a resource representing the process; 返回值是表示进程的资源; you should free it using proc_close() when you are finished with it. 你应该在完成它后使用proc_close()释放它。
#3楼
The previous answers seem all to be a little confusing or incomplete, so here is a table of the differences... 以前的答案似乎有点混乱或不完整,所以这里有一个差异表...
+----------------+-----------------+----------------+----------------+
| Command | Displays Output | Can Get Output | Gets Exit Code |
+----------------+-----------------+----------------+----------------+
| system() | Yes (as text) | Last line only | Yes |
| passthru() | Yes (raw) | No | Yes |
| exec() | No | Yes (array) | Yes |
| shell_exec() | No | Yes (string) | No |
| backticks (``) | No | Yes (string) | No |
+----------------+-----------------+----------------+----------------+
- "Displays Output" means it streams the output to the browser (or command line output if running from a command line). “显示输出”表示它将输出流式传输到浏览器(如果从命令行运行,则输出命令行输出)。
- "Can Get Output" means you can get the output of the command and assign it to a PHP variable. “可以获取输出”意味着您可以获取命令的输出并将其分配给PHP变量。
- The "exit code" is a special value returned by the command (also called the "return status"). “退出代码”是命令返回的特殊值(也称为“返回状态”)。 Zero usually means it was successful, other values are usually error codes. 零通常意味着它成功,其他值通常是错误代码。
Other misc things to be aware of: 其他需要注意的事项:
- The shell_exec() and the backticks operator do the same thing. shell_exec()和反引号运算符执行相同的操作。
- There are also proc_open() and popen() which allow you to interactively read/write streams with an executing command. 还有proc_open()和popen(),它们允许您使用执行命令以交互方式读/写流。
- Add "2>&1" to the command string if you also want to capture/display error messages. 如果您还想捕获/显示错误消息,请在命令字符串中添加“2>&1”。
- Use escapeshellcmd() to escape command arguments that may contain problem characters. 使用escapeshellcmd()来转义可能包含问题字符的命令参数。
- If passing an $output variable to exec() to store the output, if $output isn't empty, it will append the new output to it. 如果将$ output变量传递给exec()来存储输出,如果$ output不为空,它会将新输出附加到它。 So you may need to unset($output) first. 所以你可能需要先取消设置($ output)。
#4楼
They have slightly different purposes. 他们的目的略有不同。
-
exec()
is for calling a system command, and perhaps dealing with the output yourself.exec()
用于调用系统命令,也许是自己处理输出。 -
system()
is for executing a system command and immediately displaying the output - presumably text.system()
用于执行系统命令并立即显示输出 - 可能是文本。 -
passthru()
is for executing a system command which you wish the raw return from - presumably something binary.passthru()
用于执行您希望原始返回的系统命令 - 可能是二进制文件。
Regardless, I suggest you not use any of them. 无论如何,我建议你不要使用它们中的任何一个。 They all produce highly unportable code. 它们都产生高度不可移植的代码。
#5楼
It really all comes down to how you want to handle output that the command might return and whether you want your PHP script to wait for the callee program to finish or not. 它实际上归结为您希望如何处理命令可能返回的输出以及您是否希望PHP脚本等待被调用程序完成。
exec
executes a command and passes output to the caller (or returns it in an optional variable).exec
执行命令并将输出传递给调用者(或在可选变量中返回)。passthru
is similar to theexec()
function in that it executes a command .passthru
类似于exec()
函数,因为它执行命令。 This function should be used in place ofexec()
orsystem()
when the output from the Unix command is binary data which needs to be passed directly back to the browser. 当Unix命令的输出是需要直接传递回浏览器的二进制数据时,应该使用此函数代替exec()
或system()
。system
executes an external program and displays the output, but only the last line.system
执行外部程序并显示输出,但只显示最后一行。
If you need to execute a command and have all the data from the command passed directly back without any interference, use the passthru()
function. 如果您需要执行命令并将命令中的所有数据直接传回而没有任何干扰,请使用passthru()
函数。
#6楼
If you're running your PHP script from the command-line, passthru()
has one large benefit. 如果您从命令行运行PHP脚本,则passthru()
有一个很大的好处。 It will let you execute scripts/programs such as vim
, dialog
, etc, letting those programs handle control and returning to your script only when they are done. 它将允许您执行脚本/程序,如vim
, dialog
等,让这些程序处理控制并仅在完成后返回脚本。
If you use system()
or exec()
to execute those scripts/programs, it simply won't work. 如果使用system()
或exec()
来执行这些脚本/程序,它将无法工作。
Gotcha: For some reason, you can't execute less
with passthru()
in PHP. 问:出于某种原因,你不能用PHP中的passthru()
执行less
。