PHP provides 4 ways to execute system external commands: EXEC (), PassThru (), System (), Shell_exec (), respectively:
1. exec
Prototype: string exec (String $command [, Array & $output [, int & $return _var]])
EXEC executes command, but does not output all results, but returns the last line of the result, if you want all the results, you can use the second parameter to output to an array, each record of the array represents each row of the output, and if the output has 10 rows, the array has 10 records. So if you need to repeatedly output the results of calling external commands from different systems, you might want to clear this array when outputting the result of each system external command, in case of confusion. The third parameter is used to get the status code for the execution of the command, and usually the success is returned 0. Example:
[PHP]View PlainCopy
- <?php
- EXEC (' ls/home/xyw/test ');
- ?>
No output.
[PHP]View PlainCopy
- <?php
- EXEC (' ls/home/xyw/test ',$arr);
- Print_r ($arr);
- ?>
Output:
[Plain]View PlainCopy
- Array
- (
- [0] = List.txt
- [1] = List.txt.ln
- [2] = = tcpdump Chinese manual. doc
- [3] = Test1
- [4] = Bupt emblem. jpg
- [5] = top of the wave. pdf
- )
2, PassThru
Prototype: void PassThru (String $command [, int & $return _var])
Difference from exec: PassThru directly outputs the result without returning a result without using echo to view the result.
[PHP]View PlainCopy
- <?php
- PassThru ("ls test");
- ?>
Results:
[Plain]View PlainCopy
- List.txt
- List.txt.ln
- Tcpdump Chinese manual. doc
- Test1
- Bupt emblem. jpg
- The peak of the tide. pdf
3. System
Prototype: String System (String $command [, int & $return _var])
The same as PassThru, but the system returns the results and outputs. (See the return values for system and PSSTHRU)
4, Shell_exec
is a variant of the anti-apostrophe (') operator.
[Plain]View PlainCopy
- <?php
- echo ' pwd ';
- ?>
PHP executes external command functions: EXEC (), PassThru (), System (), shell_exec () comparison