Difference between exec function and shell_exec function in PHP
- 2021-07-13 04:48:53
- OfStack
These two functions are functions that execute Linux command. The difference is that the return results are different. exec can only get the last row of data, while shell_execu can get all the data.
Suppose there are the following files under the script path:
-bash-4.1# ll
Total dosage 12
-rw-rw-r--. 1 www web 133 7 Month 16 15:00 a.php
-rw-r--r--. 1 lee web 59 2 Month 29 17:05 b.php
-rw-r--r--. 1 lee web 81 3 Month 8 17:00 c.php
exec Example
<?php
/**
* exec And shell_exec Difference between
* Qiongtai blog
*/
$data = exec('/bin/ls -l');
echo '<pre>';
print_r($data);
echo '</pre>';
?>
Execution results
-rw-r--r--. 1 lee web 81 Mar 8 17:00 c.php
shell_exec Example
<?php
/**
* exec And shell_exec Difference between
* Qiongtai blog
*/
$data = shell_exec('/bin/ls -l');
echo '<pre>';
print_r($data);
echo '</pre>';
?>
Execution results
total 12
-rw-rw-r--. 1 www web 139 Jul 16 2012 a.php
-rw-r--r--. 1 lee web 59 Feb 29 17:05 b.php
-rw-r--r--. 1 lee web 81 Mar 8 17:00 c.php
Therefore, children who usually use exec function should pay attention. If you need to get all the return information, you should use shell_exec function. Of course, if the command execution result only has 1 line of return information, it doesn't matter which one you use.