tags:

views:

2759

answers:

4

What are difference? Is there a specific situation or reason for each function? If yes, can you please give some examples of those situations?

PHP.net says that they are used to execute external program. From 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?

+1  A: 

Try these for explanations

http://chipmunkninja.com

gbrandt
+1 for nice links, but did you notice they're the exact same article, young knight of Google?
Frank Crook
lol, no I didn't, I just googled, did a quick scan and answered. Next time I'll be more carefull :-)
gbrandt
+1 for the great article hidden behind that link :) .. next time summarize its contents on your *answer* instead of asking the reader to do so!
Jenko
+5  A: 

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.

Kalium
Sometimes portability has to be sacrificed for functionality. There are some things PHP just can't do well.
Frank Crook
@Frank: I concur.
bLee
Granted, but 95% of the time people want to use a shell call, the reasons are all wrong.
Kalium
@Kalium: can you elaborate more on your statement? just stating some vague percentage statistics does not convince me. I believe that using system calls to execute scripts are totally fine as long as the whole application does not depend one a bunch of scripts in the back-end.
bLee
@bLee In actual usage, I typically see system calls used to do little things like ls, cp, mv, and zip calls instead of anything that might genuinely justify it like a script invocation.
Kalium
A: 

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.

Cody Caughlan
A: 

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.

Matt