Analysis of the Usage Difference between exec and system in PHP

  • 2021-07-18 07:38:12
  • OfStack

This article tells the difference between exec and system usage in PHP, and shares it with you for your reference. The specific methods are as follows:

1 Generally speaking, calling external commands in PHP can be realized by exec and system:

system()

Prototype: string system (string command [, int return_var])
The system () function is much like that in other languages, it executes a given command, outputs, and returns a result. The second parameter is optional and is used to get the status code after the command is executed.

Return results:

Successfully returns 0,
Failure (command does not exist, etc.) returns a non-0 value

exec()

Prototype: string exec (string command [, string array [, int return_var]])
The exec () function, like system (), executes the given command, but does not output the result, but returns the last line of the result. Although it returns only the last line of the command result, you can get the complete result with the second parameter array by appending the result line by line to the end of array. So if array is not empty, it is best to clear it with unset () before calling. You can use the third parameter to get the status code of command execution only if the second parameter is specified.

Examples of use are as follows:


exec("/bin/ls -l"); 
exec("/bin/ls -l", $res); 
exec("/bin/ls -l", $res, $rc); 

I hope this article is helpful to everyone's study of PHP programming.


Related articles: