Home >Backend Development >PHP Problem >What is the difference between exec and system in php
The difference between exec and system in php: system executes an external program and displays the output, it can output and return results; exec executes an external program without outputting results but returning the last line of the result, but if you add a second Parameter array, you can also get the complete result.
Recommended: "PHP Video Tutorial"
To call external commands in PHP, you can use exec and system Implementation:
system() ---Execute the external program and display the output
Prototype:
string system (string command [, int return_var])
system( ) function is very similar to that in other languages. It executes a 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
Success returns 0,
Failure (the command does not exist, etc.) Returns a non-0 value
##exec( ) ---Execute external program
Prototype:
string exec (string command [, string array [, int return_var]])
The exec () function is similar to system() and also executes specified 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);
Related recommendations:
The above is the detailed content of What is the difference between exec and system in php. For more information, please follow other related articles on the PHP Chinese website!