What is difference between php cli and php cgi?

user1208865 picture user1208865 · Feb 16, 2012 · Viewed 52.7k times · Source

A very basic question for you. I just found two executable files php-cgi.exe and php.exe in the bin folder of the WAMP server on my laptop. I am learning PHP and could not figure out the difference. What is difference between them?

Answer

Abdala Cerqueira picture Abdala Cerqueira · Sep 3, 2014

From http://www.php-cli.com/php-cli-cgi.shtml

These are the most important differences between CLI and CGI:

  • Unlike the CGI SAPI, CLI writes no headers to the output by default
  • There are some php.ini directives which are overridden by the CLI SAPI because they do not make sense in shell environments:
    • html_errors: CLI default is FALSE
    • implicit_flush: CLI default is TRUE
    • max_execution_time: CLI default is 0 (unlimited)
    • register_argc_argv: CLI default is TRUE
  • You can have command line arguments with your script! Variable "$argc" provides you with a number of arguments passed to the application. And array "$argv" gives you an array of the actual arguments
  • There are 3 new constant defined for the shell environment: STDIN, STDOUT, STDERR. All of them are file handlers for correspondent shell devices. For example STDIN is the handler for fopen('php://stdin', 'r') . So, you can read a line from STDIN like this: $strLine = trim(fgets(STDIN));. STDIN is already defined for you by PHP CLI!
  • PHP CLI does not change the current directory to the directory of the executed script. The current directory for the script would be the directory where your type PHP CLI command.
  • There are number of USEFUL options available for PHP CLI. Which will allow you to get some valuable information about you php setup, your php script or run it in different modes.
  • In PHP 5 there were some changes in CLI and CGI filenames. In PHP 5, the CGI version was renamed to php-cgi.exe (previously php.exe) and the CLI version now sits in the main directory (previously cli/php.exe).
  • In PHP 5 it was also introduced a new mode: php-win.exe. This is equal to the CLI version, except that php-win doesn't output anything and thus provides no console (no "dos box" appears on the screen). This behaviour is similar to PHP GTK.