PHP executes external command functions: EXEC (), PassThru (), System (), shell_exec () comparison

Source: Internet
Author: User
Tags vars

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
    1. <?php
    2. EXEC (' ls/home/xyw/test ');
    3. ?>

No output.

[PHP]View PlainCopy
    1. <?php
    2. EXEC (' ls/home/xyw/test ',$arr);
    3. Print_r ($arr);
    4. ?>

Output:

[Plain]View PlainCopy
    1. Array
    2. (
    3. [0] = List.txt
    4. [1] = List.txt.ln
    5. [2] = = tcpdump Chinese manual. doc
    6. [3] = Test1
    7. [4] = Bupt emblem. jpg
    8. [5] = top of the wave. pdf
    9. )

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
    1. <?php
    2. PassThru ("ls test");
    3. ?>

Results:

[Plain]View PlainCopy
    1. List.txt
    2. List.txt.ln
    3. Tcpdump Chinese manual. doc
    4. Test1
    5. Bupt emblem. jpg
    6. 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
    1. <?php
    2. echo ' pwd ';
    3. ?>

PHP executes external command functions: EXEC (), PassThru (), System (), shell_exec () comparison

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.