What is the difference between exec and system in php

青灯夜游
Release: 2023-03-06 08:04:02
Original
1865 people have browsed it

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.

What is the difference between exec and system in php

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])
Copy after login

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]])
Copy after login

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);
Copy after login
Related recommendations: php training

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template