Assume that the script path contains the following files:
-Bash-4.1 # ll
Total usage 12
-Rw-r --. 1 www web 133 July 16 15:00 a. php
-Rw-r --. 1 lee web 59 February 29 17:05 B. php
-Rw-r --. 1 lee web 81 March 8 17:00 c. php
Let's take a look at the php Manual's explanation of the two functions:
Exec --- execute external program
Syntax: string exec (string command [, array & output [, int & return_var])
Note:
Exec () executes the command, but it does not output anything. It simply transmits the last line from the command result. If you need to execute a command, you can use the passthru () function to obtain all data from the command.
If the array parameter is given, the specified array will be filled with each line output by the command. Note: If the array already contains some elements, exec () it will be appended to the back of the array. If you do not want this function to append an element, you can call unset () before passing this array to exec ().
Exec example
The code is as follows: |
Copy code |
<? Php /** * What is the difference between exec and shell_exec? * Qiongtai blog */ $ Data = exec ('/bin/ls-L '); Echo '<pre> '; Print_r ($ data ); Echo '</pre> '; ?> |
Execution result
-Rw-r --. 1 lee web 81 Mar 8 c. phpshell_exec example
Shell_exec ()
The shell_exec () command line is actually only a variant of the anti-apostrophes (') operator. If you have written shell or Perl scripts, you can capture the output of other commands in the anti-float operator. For example, listing 1 shows how to use the extension number to count the words in each vertex (.txt) in the current directory.
In shell_exec ()
Run the same command in
The code is as follows: |
Copy code |
<? Php
$ Results = Shell_exec ( ' Wc-w *. txt ' ) ;
Echo $ Results ; ?> |
As shown in Figure 1, the results are the same as those obtained from the shell script.
What is the difference between exec and shell_exec?
The code is as follows: |
Copy code |
<? Php /** * * Qiongtai blog */ $ Data = shell_exec ('/bin/ls-L '); Echo '<pre> '; Print_r ($ data ); Echo '</pre> '; ?> |
Execution result
Total 12
-Rw-r --. 1 www web 139 Jul 16 2012 a. php
-Rw-r --. 1 lee web 59 Feb 29 :05 B. php
-Rw-r --. 1 lee web 81 Mar 8 c. php usually uses the exec function. If you need to obtain all the returned information, you should use the shell_exec function. Of course, if the command execution result contains only one row of returned information, it doesn't matter which one to use.