Three PHP Call system command function difference and contact _php Tutorial

Source: Internet
Author: User
When we execute a shell command for a Linux system, we useAlthough these three commands are capable of executing the shell commands of the Linux system, they are actually different:

System () outputs and returns the last line of the shell results.

EXEC () does not output results, returns the last line of the shell result, and all results can be saved to a returned array.

PassThru () only invokes the command and outputs the result of the command directly to the standard output device.

Same point: All can get status code of command execution

Calling external commands in PHP can be implemented in three ways:

Specialized functions provided in PHP

PHP provides a total of 3 dedicated PHP command functions that execute external commands: System (), exec (), PassThru ().

System ()

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

The system () function is almost the same in other languages, and this PHP invokes the command function to execute the given command, outputting and returning the result. The second parameter is optional and is used to get the status code after the command executes.

Example:

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

EXEC ()

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

The exec () function is similar to the system () command function for PHP, which executes the given command, but does not output the result, but instead returns the last line of the result. Although it returns only the last line of the command result, the second parameter array gives the complete result by appending the result line to the end of the array. So if the array is not empty, it is best to clear it with unset () before calling it. The third parameter can be used to obtain the status code of the command execution only if the second parameter is specified.

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 invokes the command, this PHP call system command function does not return any results, but the results of the command run directly to the standard output device. So the PassThru () function is often used to invoke programs such as Pbmplus (a tool for processing pictures under Unix, a stream of output binary raw images). It can also get the status code of the command execution.

Example:

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

The above is the three PHP call system command function Comparison of the work, I hope to be helpful to everyone.


http://www.bkjia.com/PHPjc/446317.html www.bkjia.com true http://www.bkjia.com/PHPjc/446317.html techarticle when we execute the shell command of the Linux system, we use the three commands to execute the shell command of the Linux system, but in fact they are different: the system () output and return to the last ...

  • 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.