Running PHP on the Command Line part I

Aug 11, 2009 Author: Developer

Although PHP was conceived as a tool for creating dynamic web pages, because the PHP language is very powerful, it has also become popular for writing command scripts and even desktop programs.
The Command-Line Environment

In order to use PHP from the command line, you need to have a PHP executable installed on your system. When running in a web environment, PHP is usually installed as an Apache module, but it is also possible to build a standalone program called php that can be used as a command-line interface (CLI).


Differences Between CLI and CGI Binaries

Beginning in version 4.2, PHP started to differentiate between binary programs intended for CGI and those for CLI use. Both executables provide the same language interpreter, but the CLI version includes the following changes to make it more suitable for command-line use:

  • No HTTP headers are written in the output.
  • Error messages do not contain HTML formatting.
  • The max_execution_time value is set to zero, meaning that the script can run for an unlimited amount of time.

To find out whether a php binary is a CGI or CLI version, you can run it with the v switch to see its version information. For instance, the following output is from the CLI version PHP 5.0.3:

PHP 5.0.3 (cli) (built: Dec 15 2004 08:07:57)
Copyright (c) 1997-2004 The PHP Group
Zend Engine v2.0.3, Copyright (c) 1998-2004 Zend Technologies

The value in parentheses after the version number indicates the Server Application Programming Interface (SAPI) that is in use. You can also find this value dynamically in a script by looking at the return value from the function php_sapi_name.
end of part 1


views 3651
  1. Add New Comment