Specifications
There are four techniques you can use to execute a command on the Web server. They are all
pretty similar, but there are some minor differences.
1. exec()
The exec() function has the following prototype:
string exec (string command [, array result [, int return_value]])
You pass in the command that you would like executed, for example,
exec(“ls -la”);
The exec() function has no direct output.
It returns the last line of the result of the command.
If you pass in a variable as
result, you will get back an array of strings representing
each line of the output. If you pass in a variable as
return_value, you will get the return
code.
2. passthru()
The passthru() function has the following prototype:
void passthru (string command [, int return_value])
The passthru() function directly echoes its output through to the browser. (This is use-
ful if the output is binary, for example, some kind of image data.)
It returns nothing.
The parameters work the same way as exec()’s parameters do.
3. system()
The system() function has the following prototype:
string system (string command [, int return_value])
The function echoes the output of the command to the browser. It tries to flush the output
after each line (assuming you are running PHP as a server module), which distinguishes
it from passthru().
It returns the last line of the output (upon success) or false (upon failure).
The parameters work the same way as in the other functions.
4. Backticks
We mentioned these briefly in Chapter 1, “PHP Crash Course.” These are actually an
execution operator.
They have no direct output. The result of executing the command is returned as a string,
which can then be echoed or whatever you like.
The script shown in Listing 16.5 illustrates how to use each of these in an equivalent fashion.
Advanced PHP Techniques
P
ART IV
366
21 7842 CH16 3/6/01 3:40 PM Page 366










