PHP Tutorials - Herong's Tutorial Examples - v5.17, by Herong Yang
Options to Execute External Programs
This section describes different options to execute external programs on the operting system from PHP scripts: exec(), system(), passthru(), shell_exec() and open_process().
If you want to execute an external program on the operating system in a PHP script, you have several options:
1. `` Backtick Operator - The Backtick Operator `command` function allows you to execute an external program by a given command and return its output.
`command` returns the program output stdin: The standard input is shared with the calling script. stdout: The standard output from the program is returned. stderr: The standard error is shared with the calling script. rc: The return code from the program is trashed.
2. shell_exec() Function - The shell_exec() function allows you to execute an external program by a given command and return its output. It works the same way as the Backtick Operator ``.
shell_exec(string $command) returns the program output stdin: The standard input is shared with the calling script. stdout: The standard output from the program is returned. stderr: The standard error is shared with the calling script. rc: The return code from the program is trashed.
3. exec() Function - The exec() function allows you to execute an external program by a given command and captures its output into array of lines:
exec(string $command[, array &$output[, int &$return_var]]) returns the last line of the program's standard output stdin: The standard input to the program is shared with the calling script. stdout: The standard output from the program is captured and passed to "output" as an array. stderr: The standard error is shared with the calling script. rc: The return code from the program is passed to "return_var".
4. system() Function - The system() function allows you to execute an external program by a given command and flush its output to same standard output of the calling script line by line.
system(string $command[, int &$return_var]) returns the last line of the program's standard output stdin: The standard input to the program is shared with the calling script. stdout: The standard output from the program is merged with standard output of the calling script. And every output line is flushed. stderr: The standard error is shared with the calling script. rc: The return code from the program is passed to "return_var".
5. passthru() Function - The passthru() function allows you to execute an external program by a given command and direct its output to same standard output of the calling script.
passthru(string $command[, int &$return_var]) returns void stdin: The standard input to the program is shared with the calling script. stdout: The standard output from the program is merged with standard output of the calling script. stderr: The standard error is shared with the calling script. rc: The return code from the program is passed to "return_var".
6. popen() Function - The popen() function allows you to execute an external program by a given command and control its stand input or output as a file handle.
$fh = popen(string $command, string $mode); returns a file handle represents the standard input or output depending on "mode" pclose($fh); stdin: The standard input to the program is mapped to "fh", if "mode" = 'w'. stdout: The standard output from the program is mapped to "fh", if "mode" = 'r'. stderr: The standard error is shared with the calling script. rc: The return code from the program is trashed.
7. proc_open() Function - The proc_open() function allows you to execute an external program by a given command and have full control of all input and output streams of the program.
$ph = proc_open(mixed $command, array $descriptorspec, array &$pipes [, string $cwd=NULL[, array $env=NULL[, array $other_options=NULL]]]); returns a process handle $rc = proc_close($ph) returns the return code from the program. descriptorspec: An array to describe how stdin, stdout and stderr should be mapped to their source or destinations. pipes: An array to hold file handles for stdin, stdout and stderr.
Table of Contents
Introduction and Installation of PHP
PHP Data Types and Data Literals
Variables, References, and Constants
Expressions, Operations and Type Conversions
Conditional Statements - "if" and "switch"
Loop Statements - "while", "for", and "do ... while"
Function Declaration, Arguments, and Return Values
►Interface with Operating System
$argv[] - Command Line Arguments
►Options to Execute External Programs
exec() - Execute External Programs
system() - Execute External Programs
passthru() - Execute External Programs
popen() - Execute External Programs
proc_open() - Execute External Programs
memory_get_usage() - Memory Usage Info
set_time_limit() - max_execution_time
Introduction of Class and Object
Integrating PHP with Apache Web Server
Retrieving Information from HTTP Requests
Creating and Managing Sessions in PHP Scripts
Sending and Receiving Cookies in PHP Scripts
Controlling HTTP Response Header Lines in PHP Scripts
MySQL Server Connection and Access Functions
Functions to Manage Directories, Files and Images
SOAP Extension Function and Calling Web Services
SOAP Server Functions and Examples
Localization Overview of Web Applications
Using Non-ASCII Characters in HTML Documents
Using Non-ASCII Characters as PHP Script String Literals
Receiving Non-ASCII Characters from Input Forms
"mbstring" Extension and Non-ASCII Encoding Management
Managing Non-ASCII Character Strings with MySQL Servers
Parsing and Managing HTML Documents
Configuring and Sending Out Emails
Managing PHP Engine and Modules on macOS