Home  >  Article  >  Backend Development  >  Two useful functions for executing Linux commands in PHP: exec and shell_exec

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

藏色散人
藏色散人forward
2019-12-31 17:47:544756browse

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

Calling method:

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

$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

Calling method:

$result = shell_exec($cmd);

$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!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete