Home > Backend Development > PHP Tutorial > The difference between shell_exec() and exec() functions in php

The difference between shell_exec() and exec() functions in php

藏色散人
Release: 2023-04-05 13:34:01
Original
25377 people have browsed it
Recommended manual: php complete self-study manual

The difference between shell_exec() and exec() functions in php

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

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(&#39;ls&#39;); 
  
// 显示所有文件和目录的列表
echo "<pre class="brush:php;toolbar:false">$output
"; ?>
Copy after login

Output:

gfg.php 
index.html 
demo.php
Copy after login

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

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(&#39;iamexecfunction&#39;); 
?>
Copy after login

Output:

demo.php
Copy after login
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!

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