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:
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.
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
PHP Shell Scripts on Linux/Unix
On a Linux/Unix platform, a shell script is simply a text file that contains a series of instructions that are to be processed by a specific language interpreter. The simplest shell interpreter is the Bourne Shell, sh, although these days it has been superseded by the Bourne Again Shell, bash, which is fully compatible with sh but also includes other useful features.Because the command language available in most command shells is very restrictive and often requires calls to external programs, PHP is not only a more powerful language, suitable for many tasks, but its built-in features also usually give better performance than the standard system tools.
![]() | PHP Location The php executable is usually installed to /usr/local/bin or /usr/bin, depending on whether it was installed from source or a binary package, but your actual location may vary. Try typing which php to find the location if you do not know it. |
However, for a PHP script, the first line would be
#!/bin/sh
#!/usr/local/bin/php
![]() | Hash Bang The most widely used pronunciation for the character sequence #!, found at the start of a shell script, is "hash bang," although sometimes it is also referred to as "shebang." |
If your script is to be run by any system user, the command to set global execute permission is as follows:
$ chmod u+x myscript.php
If the execute bit is not set, you can still run a file that contains a series of PHP commands through the PHP interpreter by invoking php with a filename argument. The following two commands are identical to one another (the f switch can be used for clarity but is not required):
$ chmod a+x myscript.php
$ php myscript.php
$ php f myscript.php
PHP Command Scripts on Windows
Windows does not allow an alternate command interpreter to be used in a batch script, so to execute a PHP script under Windows, you have to pass a filename argument to php.exe. The f switch is optional, so the following two commands are identical to one another:
> php.exe myscript.php
> php.exe f myscript.php
Embedding PHP Code
Just as when it is used in the web environment, PHP code in a command script needs to be embedded. Any text that does not appear inside <?php tags is sent straight to the output.Because you usually want to create a script that is entirely made up of PHP code, you must remember to begin every PHP shell script with a <?php tag. However, the embedded nature of PHP means you could create a PHP script that generates only certain elements within a largely static text file.