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 passthru() function 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() is just like the C version of the function in that it executes the given command and outputs the result. The system() call also tries to automatically flush the web server's output buffer after each line of output if PHP is running as a server module.
1. The shell_exec() function is an inbuilt function in PHP that is used to execute the commands via shell and return the complete output as a string. The exec() function is an inbuilt function in PHP that is used to execute an external program and returns the last line of the output.
exec function: This function executes a system command, stores the output in an array, and returns the last line of the output.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With