Home > Article > Backend Development > The difference between shell_exec() and exec() functions in php
Recommended manual: php complete self-study manual
shell_exec() function is a built-in function in PHP that is used to execute commands through the shell and return the complete output as a string. shell_exec is an alias for the backtick operator, used in *nix. NULL is returned if the command fails, and these values are not reliable for error checking.
Syntax:
string shell_exec( $cmd )
Parameters: This function accepts a single parameter $cmd, which is used to save the command to be executed.
Return value: This function returns the executed command, or NULL if an error occurs.
Note: This feature is disabled when PHP is running in safe mode.
Example:
<?php // 使用ls命令来执行shell_exec函数 $output = shell_exec('ls'); // 显示所有文件和目录的列表 echo "<pre class="brush:php;toolbar:false">$output"; ?>
Output:
gfg.php index.html demo.php
exec() function is a built-in function in PHP, used to execute external programs and return output the last line. It also returns NULL if the command was not run correctly.
Syntax:
string exec( $command, $output, $return_var )
Parameters: This function accepts the above three parameters, as described below:
$command: This parameter is used to save the command to be executed.
$output: This parameter is used to specify the array that will be populated with each line of output from the command.
$ return_var: The $return_var parameter comes along with the output parameters, then it returns the status of the executed command will be written to this variable.
Return value: This function returns the executed command. Please be sure to set and use the output parameters.
Example:
<?php echo exec('iamexecfunction'); ?>
Output:
demo.php
Recommended related articles:
1.exec() function execution system in php Solution to the problem of command failure
2.Introduction to the usage of PHP’s system() function & exec() function & passthru() function
Related video recommendations:
1.Dugu Jiujian (4)_PHP video tutorial
This article is about the difference between shell_exec() and exec() functions in PHP. I hope it will help you if you need Friends help!
The above is the detailed content of The difference between shell_exec() and exec() functions in php. For more information, please follow other related articles on the PHP Chinese website!