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. 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?
The previous answered seemed to all be a little confusing or incomplete, so here is a table of the differences...
Other misc things to be aware of:
As drawn from http://php.net/ && Chipmunkninja:
If you're running your PHP script from the command-line,
passthru()
has one large benefit. It will let you execute scripts/programs such asvim
,dialog
, etc, letting those programs handle control and returning to your script only when they are done.If you use
system()
orexec()
to execute those scripts/programs, it simply won't work.Gotcha: For some reason, you can't execute
less
withpassthru()
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 theexec()
function in that it executes a command . 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.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.
exec()
is for calling a system command, and perhaps dealing with the output yourself.system()
is for executing a system command and immediately displaying the output - presumably text.passthru()
is for executing a system command which you wish the raw return from - presumably something binary.Regardless, I suggest you not use any of them. They all produce highly unportable code.