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 tha
As drawn from 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.
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 |
+----------------+-----------------+----------------+----------------+
Other misc things to be aware of:
If you're running your PHP script from the command-line, passthru()
has one large benefit. 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.
If you use system()
or exec()
to execute those scripts/programs, it simply won't work.
Gotcha: For some reason, you can't execute less
with passthru()
in PHP.
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.
exec
executes a command and passes output to the caller (or returns it in an optional variable).
passthru
is similar to the exec()
function in that it executes a command . This function should be used in place of exec()
or system()
when the output from the Unix command is binary data which needs to be passed directly back to the browser.
system
executes an external program and displays the output, but only the last line.
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.
They have slightly different purposes.
Regardless, I suggest you not use any of them. They all produce highly unportable code.