Two useful functions for executing Linux commands in PHP: exec and shell_exec

藏色散人
Release: 2023-04-08 07:12:01
forward
4755 people have browsed it

Both the exec and shell_exec functions in php can be used to execute Linux commands, but their differences in usage and result return are quite large.

exec
Copy after login

Calling method:

exec($cmd.' 2>&1',$output,$status);
Copy after login

$cmd: shell script

2>&1: Indicates that error messages during the execution of the shell script will be output

$output: Array format, used to store the error message output by 2>&1

$status: The execution status of the shell script, 0 indicates success, others indicate failure

shell_exec
Copy after login

Calling method:

$result = shell_exec($cmd);
Copy after login

$cmd:shell script

$result:Execution result of shell script

Usage suggestions

If your shell script The returned result is a list, and it is recommended to use shell_exec. Correspondingly, if it returns only a certain status, it is recommended to use exec.

For more PHP related knowledge, please visit PHP Tutorial!

The above is the detailed content of Two useful functions for executing Linux commands in PHP: exec and shell_exec. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:csdn.net
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