Differences between CLI and CGI
PHP can run in different environments. The most common environment is
CGI, which runs when PHP processes an HTTP request. However, it is also possible to run a PHP script from the Terminal, in which case it is a so-called CLI (Command-line interface) task.
The most important differences between CLI and CGI
CLIdoes not write any headers to the output by default.
- There are some
php.inidirectives that are overridden in
CLI SAPIbecause they are meaningless in a shell environment:
html_errors: CLI defaults to
implicit_flush: default CLI value is
max_execution_time: default CLI value is
register_argc_argv: default CLI value is
- The script can take command line arguments! The
$argcvariable gives you the number of arguments passed to the application. And the
$argvfield gives you an array of actual arguments
- There are 3 new constants defined for the shell environment:
STDERR. All are file handlers for the corresponding shell device. For example,
STDINis a file handler for
fopen('php://stdin', 'r'). So you can read a line from
$strLine = trim(fgets(STDIN));. The
STDINis already defined for you using the
- The PHP CLI does not change the current directory to the directory of the script being executed. The current directory for the script would be the directory in which you run the PHP CLI command.
- There are a number of USEFUL options available for the PHP CLI. Which allow you to get some valuable information about your php setup, your php script or run it in different modes.
- In PHP 5 there have been some changes to the CLI and CGI file names. In PHP 5, the CGI version has been renamed to
php.exe) and the CLI version is now located in the main directory (formerly
- A new mode has also been introduced in PHP 5:
php-win.exe. This is equivalent to the CLI version, except that in
php-winnothing is printed, and thus provides no console (no "dos box" is displayed on the screen). This behaviour is similar to
Jan Barášek More about the author
The author works as a senior developer and software architect in Prague (Czech republic, Europe). He designs and manages large web applications that you know and use. Since 2009 he has gained a wealth of experience which he passes on through this website.
I'd be happy to help:Contact