Differences and connections between the three PHP calls to system command functions

Source: Internet
Author: User

We useAlthough these three commands can execute shell commands in linux, they are actually different:

System () Outputs and returns the last shell result.

Exec () does not output results. The last shell result is returned. All results can be saved to a returned array.

Passthru () only calls the command and directly outputs the command running result to the standard output device.

Similarities: both of them can obtain the command execution status code.

You can use the following three methods to call external commands in PHP:

Use special functions provided by PHP

PHP provides a total of three specialized PHP call system command functions that execute External commands: system (), exec (), passthru ().

System ()
 

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

The system () function is similar to the one in other languages. This PHP calls the system command function to execute the given command, and outputs and returns the result. The second parameter is optional and is used to obtain the status code after the command is executed.
 

Example:

System ("/usr/local/bin/webalizer ");

Exec ()

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

The exec () function is similar to the system command function called by the PHP system (). It also executes the given command, but does not output the result, but returns the last line of the result. Although it only returns the last line of the command result, the complete result can be obtained using the second parameter array by appending the result row by row to the end of the array. Therefore, if the array is not empty, we recommend that you use unset () to clear it before calling it. Only when the second parameter is specified can the third parameter be used to obtain the command execution status code.
 

Example:

Exec ("/bin/ls-l ");
Exec ("/bin/ls-l", $ res );
Exec ("/bin/ls-l", $ res, $ rc );

Passthru ()
 

Prototype: void passthru (string command [, int return_var])
Passthru () only calls the command. This PHP calls the system command function and does not return any results, but directly outputs the command running results to the standard output device as they are. Therefore, the passthru () function is often used to call programs such as pbmplus (a Unix-based image processing tool that outputs binary original image streams. It can also get the status code of command execution.
 

Example:

Header ("Content-type: image/gif ");
Passthru ("./ppmtogif hunte. ppm ");

The above is a comparison between the three PHP calls to system command functions. I hope it will be helpful to you.


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.