How PHP runs

Source: Internet
Author: User
Tags sapi
: This article describes how PHP runs. if you are interested in the PHP Tutorial, refer to it. This article describes how PHP code is interpreted and executed, and the lifecycle of PHP script running.

Overview

Start the PHP service. Strictly speaking, PHP-related processes do not need to be manually started. they run with Apache startup. Of course, you can manually restart the PHP service if you need to restart the PHP service. For example, after the code is updated in the official environment where opcode is enabled, restart PHP to re-compile the PHP code.

From a macro perspective, the implementation of the PHP kernel is to receive input data, process the data internally, and then output the results. For the PHP kernel, the PHP code we wrote is the input data received by the kernel. after the PHP kernel receives the code data, it parses and computes the code we wrote, finally, return the corresponding calculation result.

However, unlike the common C language code, to execute PHP code, you must first "translate" the PHP code into a machine language to execute the corresponding functions. To perform the "translation" step, the PHP kernel needs to perform the lexical analysis, syntax analysis, and other steps. Finally, it is handed over to the PHP kernel's Zend Engine for sequential execution.

Lexical analysis

Separate PHP code into multiple "tokens)

Syntax analysis
Converts a "unit" to an action that Zend Engine can perform.

Zend Engine execution
Perform the operations obtained by syntax analysis in sequence.

All PHP programs (CGI/CLI) start with the SAPI (Server Application Programming Interface) Interface. SAPI refers to the programming interface of a specific PHP application. For example, Apache mod_php.

PHP will go through two major phases after execution: The start phase before processing the request and the end phase after the request.

Start stage

The entire starting phase of PHP goes through two phases: module initialization and Module activation.

MINIT

That is, the module initialization phase occurs throughout the lifecycle or the entire execution process of the command line program after Apache/Nginx is started. This phase is only performed once.

RINIT
Module activation occurs in the request phase. Perform initialization, such as registering constants and defining classes used by modules.

The module can use the following macro to implement these callback functions:

PHP_MINIT_FUNCTION (myphpextension) {// registers constants or classes and other initialization operations return SUCCESS;} PHP_RINIT_FUNCTION (myphpextension) {// for example, record the request start time // then record the end time at the end of the request. In this way, we can record the time it took to process the request: return SUCCESS ;}

After the PHP script request is processed, it enters the end stage. Generally, when the script is executed to the end or the exit or die function is called, PHP enters the end stage.

End stage

The PHP end stage consists of the disabled module and the disabled module.

RSHUTDOWN
Disable the module (corresponding to RINIT)

MSHUTDOWN
Close the module (corresponding to MINIT)

PHP in CLI/CGI mode belongs to the SAPI mode of a single process. This means that the PHP script is closed once executed, and all variables and functions cannot be used. That is, in CGI mode, variables in the same PHP file cannot be used in other PHP files.

The following example shows the SAPI lifecycle of a single-thread PHP.

Single-threaded SAPI lifecycle
For example:

php -f test.php

Call various extended MINIT module initialization
Request test. php
Call the extended RINIT module for activation
Run test. php.
Call the extended RSHUTDOWN stop module
Run test. php to clear variables and memory.
Call the MSHUTDOWN function of each extension to close the module.
Stop PHP execution

The above is a simple execution process, which will be supplemented below.

Before calling the module initialization of each module, PHP has an initialization process, including:
Initialize several global variables
In most cases, it is set to NULL.

Initialize several constants
The constants here are some constants of PHP itself.

Initialize Zend Engine and core components
The initialization operations here include memory management initialization, global function pointer initialization, lexical analysis, syntax analysis for PHP source files, and assignment of function pointers executed by intermediate code, initialize several HashTable (such as function tables and constant tables), prepare for INI file parsing, prepare for PHP source file parsing, and register built-in functions, standard constants, and GLOBALS global variables.

Parsing php. ini
Read the php. ini file, set the configuration parameters, load the zend extension, and register the PHP extension function.

Global function initialization
Initialize some frequently used global variables in the user space, such as $ \ _ GET, $ \ _ POST, and $ \ _ FILES.

Initialize static building modules and shared modules (MINIT)
Initialize the default loaded modules.
Initialize the module and perform the following operations:
Register the module to the registered module list
Register the functions contained in each module to the function table.

Disable functions and classes

The zend_disable_function is called to delete the functions represented by the disable_functions variable in the configuration file of PHP from the CG (function_table) function table.

Activate Zend Engine
Use the init_compiler function to initialize the compiler.

Activate SAPI
Use the sapi_activate function to initialize SG (sapi_headers) and SG (request_info), and set some content for the HTTP request method.

Environment initialization
Initialize some environment variables required for the user control. Including the server environment and request data environment.

Module Request Initialization
PHP calls the zend_activate_modules function to traverse all modules registered in the module_registry variable and calls its RINIT method to implement the request initialization of the module.

After processing the file-related content, PHP will call php_request_startup for Request Initialization:

Activate Zend Engine
Activate SAPI
Environment initialization
Module Request Initialization

Code running
After all the preceding preparations are completed, run the PHP program. PHP performs lexical analysis, syntax analysis, and intermediate code generation through zend_compile_file, and returns all intermediate code of this file. If the resolved file generates valid intermediate code, zend_excute is called to execute the intermediate code .. If exceptions occur during execution and you have defined the handling of these exceptions, call these exception handling functions. After all operations are completed, PHP returns the result through EG (return_value_ptr_ptr.

DEACTIVATION (close request)
The PHP closing request process is a collection of several closing operations, which exist in the php_request_shutdown function. This includes:

Call all functions registered through register_shutdown_function. These functions are added to the user space when they are disabled.
Run all the available _ destruct functions. The Destructor here includes the destructor of all objects in the object pool (EG (objects_store) and the destructor of each element in the EG (symbol_table.
Click it to fl all output data.
Send an HTTP response header.
Destroys the global variable table (PG (http_globals.
Use the zend_deactivate function to disable the lexical analyzer, syntax analyzer, and intermediate code actuator.
Call the post-RSHUTDOWN function for each extension. Only the pointer of each extended post_deactivate_func function is NULL.
Disable SAPI and destroy SG (sapi_headers) and SG (request_info) through sapi_deactivate.
Disable the package of the stream and the filter of the stream.
Disable memory management.
Reset the maximum execution time

End
When PHP finishes a process, it will call the sapi_flush function to refresh the final content. Call the zend_shutdown function to disable the Zend Engine.

Reference: [http://www.php-internals.com/book/] (http://www.php-internals.com/book)

The above introduces how PHP runs, including some content, and hopes to help friends who are interested in PHP tutorials.

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.