SAPI, or Server Application Programming Interface, is the interface through which PHP interacts with other applications. PHP scripts can be executed in many ways, such as through a Web server, on the command line, or embedded in other programs.
SAPI provides an interface for external communication. Common SAPI include: CGI, fast-CGI, CLI, DLL of Apache module, ISAPI, etc.
CGI
CGI is the Common Gateway Interface, it is a program, commonly speaking, CGI is like a bridge between the WEB page and the implementation of the WEB server, it passes the HTML received instructions to the implementation of the server, The results of the server’s execution are then returned to the HTML.
CGI is cross-platform and can be implemented on almost any operating system.
When a CGI connection request is received, it creates a CGI child process, activates a CGI process, processes the request, and terminates the child process. This is called fork-and-execute mode.
To sum up, there are as many CGI child processes as there are connection requests on a CGI server, and repeated loading of child processes can lead to poor CGI performance. A large number of user requests occupy system resources, such as memory and CPU time, resulting in low performance.
FastCGI
Fast cgi is an upgraded version of CGI. FastCGI is like a long-live CGI that can be activated and executed forever.
How FastCGI works: The FastCGI process manager (IIS ISAPI or Apache Module) is loaded at Web Server startup. The FastCGI process manager initializes itself, starts multiple CGI interpreter processes (multiple phP-CGI visible) and waits for connections from the Web Server; When a client request arrives at the Web Server, the FastCGI process manager selects and connects to a CGI interpreter. The Web Server sends CGI environment variables and standard input to the FastCGI child process php-cgi; The FastCGI child process returns the standard output and error message from the same connection to the Web Server. When the FastCGI child closes the connection, the request is processed. The FastCGI child process then waits for and processes the next connection from the FastCGI process manager running in the Web Server. In CGI mode, php-cgi exits at this point.
APACHE2HANDLER
PHP as a module of Apache, Apache server after the system starts, pre-generated multiple process copies reside in memory, once there is a request, immediately use these spare child process for processing, so there is no delay caused by the generation of child process. These server replicas do not exit immediately after processing an HTTP request, but stay on the machine waiting for the next request. Faster response to client browser requests and higher performance.
DLL for apache module
This runtime mode is what we used to use most often with Apache servers in Windows environments, whereas in modular (DLL) PHP is up and running with a Web server. (CGI is an extension of Apache to speed up the running of PHP)
ISAPI
ISAPI (Internet Server Application Program Interface) is an Internet service-oriented API provided by Microsoft. An ISAPI DLL, can be activated by the user long resident memory, waiting for the user another request, but also in a DLL to set up a number of user request processing functions, in addition to THE ISAPI DLL application and WWW server in the same process, the efficiency is significantly higher than CGI.
CLI
The CLI (Full name: command-line interface) is the most widely used user interface before the graphical user interface (GUI). It usually does not support the mouse. Users enter commands through the keyboard and the computer executes the commands after receiving them. Some call it CUI (Character User interface).
Execute the process
A piece of PHP code goes through lexical parsing, syntax parsing, etc., and is translated into instructions (Opcode), which are then executed sequentially by the Zend virtual machine. PHP itself is implemented in C language, so the final call is also C language functions, in fact, we can regard PHP as a SOFTWARE developed in C language.
The core of PHP execution is the translated instructions, called Opcode, which is the basic unit of PHP program execution.
In computer science, Operation Code is used to describe the part of machine Code in machine-language instructions that specifies an Operation to be performed. The format and specification of the instructions that make up Opcode are specified by the processor’s instruction specification.
An opcode consists of two arguments (op1, op2), a return value, and a handler. The PHP program is ultimately translated into a sequence of opcode handling functions.
ZEND_ASSIGN_SPEC_CV_CV_HANDLER: allocates variables (a=a=a=b); ZEND_DO_FCALL_BY_NAME_SPEC_HANDLER: function call; ZEND_CONCAT_SPEC_CV_CV_HANDLER: string concatenation A.A.A.B; ZEND_ADD_SPEC_CV_CONST_HANDLER: addition a+2; ZENDISEQUALSPECCVCONST: Judge equal a+2; ZEND_IS_EQUAL_SPEC_CV_CONST: Determine equal a+2; ZENDISEQUALSPECCVCONST: judge equal a==1; ZEND_IS_IDENTICAL_SPEC_CV_CONST: $a===1