Home > Backend Development > PHP7 > body text

Do you understand the execution process of PHP7?

藏色散人
Release: 2023-02-18 08:10:01
forward
1837 people have browsed it

PHP is an interpreted language, and its execution process needs to be compiled into intermediate code first, and then go through a specific virtual machine, translated into specific instructions to be executed. The execution process is as follows:
PHP Code=> Token => Abstract Syntax Tree=> Opcodes => Execution

The contents of each step are as follows:
Source code Token
Token is obtained through lexical analysis. The PHP code is cut into meaningful identifiers. PHP7 has a total of 137 Tokens, which are defined in the zend_language_parser.h file.

Convert Token into Abstract Syntax Tree (AST) based on syntax analyzer
Token is a word block, but a single word block cannot express complete semantics and needs to be organized with the help of certain rules in series. Therefore, the parser needs to match the Token according to the grammar and concatenate the Tokens. The product after the syntax analyzer concatenates the Tokens is an Abstract Syntax Tree (AST).

AST is a new feature of PHP7 version. The previous version of PHP code did not generate AST during the execution process. Its main function is to decouple the PHP compiler and interpreter and improve maintainability.

Convert the syntax tree into Opcode
You need to convert the syntax tree into Opcode before it can be directly executed by the engine.

Execution Opcodes
opcodes is a collection of opcodes and is the intermediate code during PHP execution. One of the more common PHP engineering optimization measures is "turn on opcache", which refers to the technology of caching opcodes. By eliminating the stage from source code to opcode, the engine directly executes the cached opacode to improve performance.

PHP7 kernel architecture
Do you understand the execution process of PHP7?

zend engine
Lexical/grammatical analysis, AST compilation and execution of opcodes are all implemented in the Zend engine. In addition, PHP's variable design, memory management, process management, etc. are also implemented at the engine layer.

PHP Layer
The zend engine provides basic capabilities for PHP, and interactions from the outside need to be handled through the PHP layer.

SAPI
Abbreviation for server API, which contains the cli SAPI and fpm SAPI of the scenario. External modules can interact with PHP as long as they adhere to the defined SAPI protocol.

Extension part
Based on the core capabilities and interface specifications provided by the zend engine, development and expansion can be carried out.

PHP 7 source code structure
The main source directories of PHP 7 are: sapi, Zend, main, ext and TSRM.
sapi directory
sapi directory is an abstraction of the input and output layers, and is the specification for PHP to provide external services.
Several commonly used SAPIs:
1) apache2handler: Apache extension, compiled to generate a dynamic link library, configured under Apache. When there is an http request to Apache, this dynamic link library will be called according to the configuration to execute the PHP code and complete the interaction with PHP.
2) cgi-fcgi: After compilation, an executable program that supports the CGI protocol is generated. The webserver (such as NGINX) passes the request to the CGI process through the CGI protocol. The CGI process executes the corresponding code according to the request and returns the execution result to the webserver.
3) fpm-fcgi: fpm is the FastCGI process manager. Taking the NGINX server as an example, when a request is sent to the NGINX server, NGINX hands the request to the php-fpm process according to the FastCGI protocol.
4) cli: PHP’s command line interactive interface
Zend directory
The Zend directory is the core code of PHP. Memory management, garbage collection, process management, variables, array implementation, etc. in PHP are all in the source code of this directory.

main directory
The main directory is the glue between the SAPI layer and the Zend layer. The Zend layer implements the compilation and execution of PHP scripts, the sapi layer implements the abstraction of input and output, and the main directory plays a connecting role between them. Following the above, it parses the SAPI request and analyzes the script file and parameters to be executed. Next, it completes the necessary module initialization and other work before calling the zend engine.

ext directory
ext is a directory related to PHP extensions. Commonly used array, str, pdo and other series of functions are defined here.

TSRM
TSRM (Thread Safe Resource Manager) - Thread Safe Resource Manager is used to ensure the security of resource sharing.

The above is the detailed content of Do you understand the execution process of PHP7?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:segmentfault.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template