49

Anybody please tell me. I want know the different between the exec(), shell_exec, system() and passthru() function?

I search from php.net unable to get the answers I need.

halfer
  • 19,824
  • 17
  • 99
  • 186
Developer
  • 2,676
  • 8
  • 43
  • 65
  • 1
    As far as I can see, correct description (with differences) are provided for each on PHP webiste. http://uk3.php.net/exec – marekful Nov 19 '13 at 13:25
  • http://stackoverflow.com/questions/10828707/what-are-the-differences-of-system-exec-and-shell-exec-in-php – Krish R Nov 19 '13 at 13:27
  • Can u sey me what is the use of passthru() ? Send me reference link which i will find my answer with example – Developer Nov 19 '13 at 13:27
  • From PHP documentation for passthru: "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." http://uk3.php.net/manual/en/function.passthru.php – marekful Nov 19 '13 at 13:30

2 Answers2

97
  • exec only returns the last line of the generated output.
  • shell_exec returns the full output of the command, when the command finished running.
  • system immediately shows all output, and is used to show text.
  • passthru also returns output immediately, but is used for binary data. passthru displays raw data.

With both exec and shell_exec it is possible to handle the output yourself, while system and passthru won't let you customize it and immediately display the output.

A more detailed comparison can be found here.

Patrick Kostjens
  • 5,065
  • 6
  • 29
  • 46
  • Would be awesome if you had an example – Kellen Stuart Feb 06 '18 at 23:23
  • `system` return false when there was an error. Does this mean when the exit code is `!== 0` it will return false? So I do not have to actually check the `$retval` if I just want to check of the command suceeded? – redanimalwar Jan 22 '21 at 09:53
4

passthru is used for returning binary data instead of ascii. A typical example is where an image manipulation program is returning an image instead of text data.

See PHP - exec() vs system() vs passthru() for more info

Also see php shell_exec() vs exec().

Community
  • 1
  • 1
vogomatix
  • 4,856
  • 2
  • 23
  • 46