The difference between Php.exe and Php-win.exe and Php-cgi.exe

Source: Internet
Author: User
Tags sapi stdin
command line mode for PHP

Starting with version 4.3.0, PHP provides a new type of SAPI (Server application Programming Interface, service-side application programming port) support, called CLI, which means command line Interface, which is life The line interface. As the name implies, the SAPI module is mainly used as the development shell application of PHP. There are many differences between CLI SAPI and other SAPI modules, as we will elaborate in this chapter. It is worth mentioning that the CLI and CGI are different SAPI, although there are many common behaviors between them.

The CLI SAPI was first released with PHP version 4.2.0, but it is still an experimental version and needs to be added--ENABLE-CLI parameters when running./configure. Starting with PHP version 4.3.0, the CLI SAPI becomes a formal module,--ENABLE-CLI parameters are set to on by default, or they can be masked with parameter--disable-cli.

Starting with PHP 4.3.0, the file name, location, and presence of the cli/cgi binary execution file will vary according to the installation of PHP on the system. By default, when you run make, both the CGI and the CLI are compiled and placed under the sapi/cgi/php and sapi/cli/php of the PHP source file directory, respectively. Notice that all two files are named for PHP. What happens during make install depends on the configuration line. If a SAPI module, such as APXS, is selected when configured, or if the--disable-cgi parameter is used, the CLI will be copied to {prefix}/bin/php in the process of make install unless the CGI has been placed in that position. Therefore, for example, if there is a--with--apxs in the configuration row, the CLI will be copied to {prefix}/bin/php during make install. If you want to undo the installation of the CGI execution file, run make install-cli after install. Alternatively, you can add the--disable-cgi parameter to the configuration line.

Note:

Because--enable-cli and--enable-cgi are also valid by default, you do not have to configure the line to add--enable-cli so that the CLI is copied to {prefix}/bin/php during make install.

In the Windows release package between PHP 4.2.0 to PHP 4.2.3, the CLI file name is Php-cli.exe, and the Php.exe under the same folder is CGI. Starting with the PHP 4.3.0 version, the CLI executable file in Windows release package is Php.exe and placed under a separate folder named CLI, that is, Cli/php.exe. In PHP 5, the CLI exists in the home folder, named Php.exe, and the CGI version is named Php-cgi.exe.

From PHP 5, a new file named Php-win.exe is released with the package. It is equivalent to the CLI version, but Php-win does not output any content and does not provide a console (no "DOS window" is ejected). This approach is similar to PHP-GTK. You need to configure it with the--ENABLE-CLI-WIN32 option.

Note: How to know which SAPI you are using.

At the command line, running Php-v can tell if the PHP is CGI or CLI. Refer to the function php_sapi_name () and constant PHP_SAPI.

Note:

The Unix man page was added to the PHP 4.3.2. You can type man php on the command line to view it.

The following are significant differences between CLI SAPI and other SAPI modules:

Unlike the CGI SAPI, its output does not have any header information.

Although the CGI SAPI provides a way to suppress HTTP header information, there is no similar method in the CLI SAPI to open the output of HTTP header information.

The CLI defaults to quiet mode, but in order to ensure compatibility, the-Q and--no-header parameters remain for backward compatibility, making it possible to use old CGI scripts.

At run time, the working directory is not changed to the current directory of the script (you can use the-C and--no-chdir parameters to be compatible with the CGI mode).

Error message (non-HTML format) for output plain text when an error occurs.

The CLI SAPI enforces some of the settings in php.ini because these settings are meaningless in the shell environment.

Overwrite php.ini settings option "Meaningless HTML tags can make error messages messy, so it is very difficult to read the error information under the shell." The default value for this option is therefore changed to  false.
Setting Options CLI SAPI Default value Notes
html_errors FALSE
implicit_flush TRUE in command line mode, all from  print ()   and  echo () &NB The output of the SP will be immediately written to the output without any buffer operation. If you want to defer or control standard output, you can still use  output buffering  to set items.
max_execution_time 0 (Infinity) Given the infinite possibilities of using PHP in a shell environment, the maximum elapsed time is set to an infinite value. Applications developed for the web may take only a few seconds to run, and the shell application may run much longer.
register_argc_argv TRUE

Because this setting is  true, you will always be able to  cli SAPI&N BSP; access to  ARGC (number of application parameters passed) and  ARGV (array containing actual arguments).

For PHP 4.3.0, when using  cli sapi , the PHP variable $argc  and   $ARGV   has been registered and the corresponding value has been set. In the previous version, the establishment of these two variables in the  CGI    module   version depended on setting the PHP setup option register_globals to  on. In addition to versioning and register_globals  settings, you can access them at any time by calling $_server or   $HTTP _server_vars . For example: $_server[' argv '

Note:

These settings cannot be initialized to other values in the settings file php.ini or in any other specified file. These defaults are limited to changes after all other settings files have been parsed. However, their values can be changed while the program is running (although these settings are meaningless for the running process).

To mitigate the work in the shell environment, we have defined the following constants:

CLI-specific constants
constant Name Description
Stdin An open stream that points to stdin. You can call it in the following ways: <?php

$stdin = fopen (' Php://stdin ', ' R ');

?> If you want to read one line of content from stdin, you can use the <?php
$line = Trim (fgets (STDIN)); Read a row from STDIN
FSCANF (STDIN, "%d", $number); Reading numbers from STDIN
?>
STDOUT An open stream that points to stdout. You can call it in the following ways: <?php

$stdout = fopen (' php://stdout ', ' W ');

?>
STDERR An open stream that points to stderr. You can call it in the following ways: <?php

$stderr = fopen (' Php://stderr ', ' W ');

?>

With these constants, you do not have to establish a stream of your own that points to such stderr, simply using these constants instead of the stream pointing:

Php-r ' fwrite (STDERR, "STDERR
"); '
You do not have to close these streams yourself, and PHP does this automatically.

CLI SAPI does not change the current directory to the directory where the script is running.

The following example shows the difference between this module and the CGI SAPI module: <?php
A simple test program called test.php
Echo getcwd (), "";
?>

When using the CGI version, the output is

$ pwd/
tmp

$ php-cgi-f another_directory/test.php
/tmp/another_directory

You can see that PHP has changed the current directory to the directory where the script just ran.

Using CLI SAPI mode, get:

$ pwd/
tmp

$ php-q another_directory/test.php/
tmp
This makes it much easier to write the shell tool with PHP.

Note:

You can add the-c parameter to the CGI SAPI when the command line is run to enable it to support the functionality of the CLI SAPI.

The following are the command-line-mode option parameters provided by the PHP binaries (that is, the  php.exe  program) that can be run at any time by running PHP commands with-h  parameters to query these parameters.

usage:php [Options] [f] <file> [--] [args ...]
       PHP [Options]-R <code> [--] [args ...]
       PHP [Options] [b <begin_code>] r <code> [e <end_code>] [--] [args ...]
       PHP [Options] [b <begin_code>]-F <file> [e <end_code>] [--] [args ...]
       PHP [Options]--[args ...] PHP [Options]-a-a Run interactively-c <path>|<file> look to php.ini file in this direct               Ory-n No php.ini file'll be used-d Foo[=bar] Define INI entry foo with value ' bar '-E
  Generate extended information for debugger/profiler-f <file> Parse <file>.               -H This help-i PHP information-l Syntax Check Only (lint)-M Show compiled in Modules-r <code> Run PHP <code> without using script tags <?..? >-B <begin_code> Run PHP <begin_code> before processing INPUT lines-r <code> Run PHP <code> for every input line-f <file> Parse and Execute               ;file> for every input line-e <end_code> Run PHP <end_code> over processing all input lines-h
  Hide any passed arguments from external tools.
  -S Display colour syntax highlighted source.
  -V Version number-w Display Source with stripped comments and whitespace.

  -Z <file> Load Zend extension <file>          Args ... Arguments passed to script.
 Use--args when the argument starts With-or script is read from stdin

The CLI SAPI module has the following three different ways to get the PHP code to run:

Let PHP run the specified file.

PHP my_script.php

php-f MY_SC

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.