Php executes external command functions: exec (), passthru (), system (), shell

Source: Internet
Author: User
PHP provides four methods for executing system external commands: exec (), passthru (), system (), and shell_exec (). The following describes the exec prototype: 1: stringexec (string $ command [, array $ output [, int $ return_var]) exec executes the command, but does not output all results, but returns the final result

PHP provides four methods for executing system external commands: exec (), passthru (), system (), and shell_exec (). The following describes the exec prototype: 1: string exec (string $ command [, array $ output [, int $ return_var]) exec executes the command, but does not output all results, but returns the final result.

PHP provides four methods for executing system external commands: exec (), passthru (), system (), and shell_exec:

1. exec

Prototype: string exec (string $ command [, array & $ output [, int & $ return_var])

Exec executes the command, but does not output all results, but returns the last line of the result. If you want to get all results, you can use the second parameter to output it to an array, each record in the array represents each row of output. If there are 10 rows of output results, the array has 10 records. So if you need to repeatedly output the results of calling different system external commands, you 'd better clear the array when outputting the results of each system external command to prevent confusion. The third parameter is used to obtain the status code for command execution. Generally, 0 is returned for successful execution. Example:

 
No output.
 
Output:
Array ([0] => list.txt [1] => list.txt. ln [2] => tcpdumpchinese album .doc [3] => test1 [4] => beijiao shipper hui.jpg [5] => surging tide)
2. passthru

Prototype: void passthru (string $ command [, int & $ return_var])

Difference from exec: passthru directly outputs the result without returning the result. You do not need to use echo to view the result.

 
Result:
List.txtlist.txt.lntcpdumpchinese album .doctest1beijiao hui.jpg
3. system

Prototype: string system (string $ command [, int & $ return_var])

It is basically the same as passthru, but the system returns the result and outputs it. (View the returned values of system and pssthru)

4. shell_exec

Is a variant of the anti-apostrophes (') operator.

 
Reference: http://hi.baidu.com/cszhi/item/d71d61f22feefe16ce9f32d1

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.