51

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.

4
  • 1
    As far as I can see, correct description (with differences) are provided for each on PHP webiste. uk3.php.net/exec Commented Nov 19, 2013 at 13:25
  • stackoverflow.com/questions/10828707/… Commented Nov 19, 2013 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 Commented Nov 19, 2013 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." uk3.php.net/manual/en/function.passthru.php Commented Nov 19, 2013 at 13:30

2 Answers 2

98
  • 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.

Sign up to request clarification or add additional context in comments.

2 Comments

Would be awesome if you had an example
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?
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().

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.