When we execute the shell command of the Linux system, we will use Although these three commands can all execute the shell command of the Linux system, they are actually different:
system() outputs and returns the last line of shell results.
exec() does not output results and returns the last line of shell results. All results can be saved in a returned array.
passthru() only calls the command and outputs the result of the command directly to the standard output device as is.
Same point: you can get the status code of command execution
To call external commands in PHP, you can use the following three methods:
Use PHP Specialized functions provided
PHP provides a total of 3 specialized PHP calling system command functions for executing external commands: system(), exec(), passthru().
system()
Prototype: string system (string command [, int return_var])
system() function Much like in other languages, this PHP calls the system command function to execute the given command, output and return the results. The second parameter is optional and is used to get the status code after the command is executed.
Example:
system("/usr/local/bin/webalizer/webalizer");
exec()
Prototype: string exec(string command [, string array [, int return_var]])
exec() function and system( ) This PHP calls the system command function similarly. It also executes the given command, but does not output the result, but returns the last line of the result. Although it only returns the last line of the command result, using the second parameter array can get the complete result by appending the results line by line to the end of the array. So if the array is not empty, it is best to use unset() to clear it before calling it. Only when the second parameter is specified, the third parameter can be used to obtain the status code of command execution.
Example:
exec("/bin/ls -l");
exec("/bin/ls -l", $ res);
exec("/bin/ls -l", $res, $rc);
passthru()
Prototype: void passthru (string command [, int return_var])
passthru () only calls the command. This PHP call system command function does not return any results, but outputs the running results of the command directly to the standard output device as is. Therefore, the passthru() function is often used to call programs like pbmplus (a tool for processing images under Unix that outputs a binary stream of original images). It can also get the status code of command execution.
Example:
header("Content-type: image/gif");
passthru("./ppmtogif hunte.ppm") ;
The above is a comparison of the performance of three PHP calling system command functions. I hope it will be helpful to everyone.