Home > Article > Backend Development > Analysis of the difference between exec and system usage in PHP
system()
Prototype: string system (string command [, int return_var])
The system() function is similar to that in other languages. It executes the given command, outputs and returns the result. The second parameter is optional and is used to get the status code after the command is executed.
Return result:
Return 0 successfully,
Failure (command does not exist, etc.) Return non-zero value
exec()
Prototype: string exec (string command [, string array [, int return_var]])
exec () function Similar to system(), 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.
Usage examples are as follows:
exec("/bin/ls -l");
exec("/bin/ls -l", $res);
exec("/bin/ls -l", $res, $ rc);