PHP is an interpreted language. Unlike compiled languages, the compilation result is the instruction of the current CPU system. PHP source code can only be directly executed by the zend virtual machine when it is compiled into opcode.
The following is a brief description of the PHP7 language execution principle:
1. The source code first uses the lexical analyzer implemented by Re2c for lexical analysis, and cuts the source code into multiple String unit, the divided string is called Token;
2. The syntax analyzer implemented based on Bison generates an abstract syntax tree from Token and code that conforms to BNF grammar rules;
3. Abstract syntax tree (AST) is compiled to generate opcode; process.
Not only that, unlike Java and other languages that are resident in memory, the memory will be released immediately after the PHP code is executed, and basically all data will be destroyed at this time (only a very small amount of data will be cached).
The advantage of this execution mechanism is that it effectively avoids memory leaks and the memory recycling mechanism is simpler. The disadvantage is that each PHP request must repeat the request-translation-execution process. Note: Memory Leak means that the dynamically allocated heap memory in the program is not released or cannot be released for some reason, causing a waste of system memory, causing the program to slow down or even crash the system. Wait for serious consequences. In order to make up for the shortcoming of not being resident in memory, the opcode cache is introduced. The zend virtual machine caches the PHP code compilation result of the first execution into the memory or hard disk, and directly reads the cache when the next time this part of the code is executed. , which can improve PHP running speed to a certain extent.Reference materials
1. Chen Lei, PHP7 underlying design and source code implementation
2. Lie Xusong, Chen Wen, PHP core technology and best practices
The above is the detailed content of A brief introduction to the underlying design of PHP7 01-PHP7 language execution principle. For more information, please follow other related articles on the PHP Chinese website!