PHP??™s Program Execution Functions
This section introduces several functions (in addition to the backticks execution
operator) used to execute system-level programs via a PHP script. Although at first
glance they all appear to be operationally identical, each offers its own syntactical
nuances.
Executing a System-Level Command
The exec() function is best-suited for executing an operating system??“level application
intended to continue in the server background. Its prototype follows:
string exec(string command [, array output [, int return_var]])
Although the last line of output will be returned, chances are that you??™d like to have
all of the output returned for review; you can do this by including the optional parameter
output, which will be populated with each line of output upon completion of the
command specified by exec(). In addition, you can discover the executed command??™s
return status by including the optional parameter return_var.
Although I could take the easy way out and demonstrate how exec() can be used
to execute an ls command (dir for the Windows folks), returning the directory listing,
it??™s more informative to offer a somewhat more practical example: how to call a Perl
script from PHP. Consider the following Perl script (languages.pl):
#! /usr/bin/perl
my @languages = qw[perl php python java c];
foreach $language (@languages) {
print $language.
Pages:
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380