有什么不同?

每个功能都有特定的情况或原因吗?如果是,你能举一些例子吗?

net说它们是用来执行外部程序的。见参考 从我看到的例子中,我没有看到任何明显的区别。

如果我要简单地运行一个脚本(bash或python),您建议我使用哪个函数?


它们的目的略有不同。

Exec()用于调用系统命令,可能还需要自己处理输出。 System()用于执行系统命令并立即显示输出——假定是文本。 Passthru()用于执行您希望从中返回原始数据的系统命令——假定是二进制文件。

无论如何,我建议你不要使用它们中的任何一个。它们都产生高度不可移植的代码。


实际上,这完全取决于您希望如何处理命令可能返回的输出,以及您是否希望PHP脚本等待被调用方程序完成。

Exec执行命令并将输出传递给调用者(或以可选变量的形式返回)。 Passthru类似于exec()函数,因为它执行一个命令。当Unix命令的输出是需要直接传递回浏览器的二进制数据时,这个函数应该用来代替exec()或system()。 系统执行一个外部程序并显示输出,但只显示最后一行。

如果需要执行一个命令,并将该命令中的所有数据直接传回而不受任何干扰,请使用passthru()函数。


如果从命令行运行PHP脚本,passthru()有一个很大的好处。它将允许你执行脚本/程序,如vim, dialog等,让这些程序处理控制,只有当它们完成时才返回到你的脚本。

如果您使用system()或exec()来执行这些脚本/程序,则根本无法工作。

明白了:由于某些原因,在PHP中不能使用passthru()执行更少的操作。


摘自http://php.net/ && Chipmunkninja:

The system() Function 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. 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). The return of this function is the last line of output from the program, if it emits text output. The exec() Function 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. 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 Most of the programs we have been executing thus far have been, more or less, real programs1. However, the environment in which Windows and Unix users operate is actually much richer than this. Windows users have the option of using the Windows Command Prompt program, cmd.exe This program is known as a command shell. The passthru() Function One fascinating function that PHP provides similar to those we have seen so far is the passthru function. 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 (i.e. either HTTP in a web server scenario, or the shell in a command line version of PHP). The proc_open() Function and popen() function proc_open() is similar to popen() but provides a much greater degree of control over the program execution. cmd is the command to be executed by the 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. pipes will be set to an indexed array of file pointers that correspond to PHP's end of any pipes that are created. The return value is a resource representing the process; you should free it using proc_close() when you are finished with it.


前面的答案似乎都有点混乱或不完整,所以这里有一个差异表…

+----------------+-----------------+----------------+----------------+
|    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             |
+----------------+-----------------+----------------+----------------+

“显示输出”意味着它将输出流输出到浏览器(如果从命令行运行,则为命令行输出)。 “Can Get Output”意味着您可以获得命令的输出并将其分配给一个PHP变量。 “退出码”是命令返回的一个特殊值(也称为“返回状态”)。0通常表示成功,其他值通常是错误代码。

其他需要注意的杂项:

The shell_exec() and the backticks operator do the same thing. There are also proc_open() and popen() which allow you to interactively read/write streams with an executing command. Add "2>&1" to the command string if you also want to capture/display error messages. Use escapeshellcmd() to escape command arguments that may contain problem characters. If passing an $output variable to exec() to store the output, if $output isn't empty, it will append the new output to it. So you may need to unset($output) first.