We all know the execution process of PHP scripts. First, the Zend engine compiles the PHP source code into an opcode sequence, and then the Zend VM interprets and executes it. The general compilation process is to perform lexical analysis and syntax analysis first, and then compile. After syntax analysis, there is a concept of Abstract Syntax Tree (Abstract Syntax Tree or abbreviated as AST), which is regarded as the output of syntax analysis. The subsequent compilation process is performed by the compiler based on AST.
But PHP is special. The Zend engine directly generates opcode after syntax analysis, without generating AST. The biggest advantage of doing this is that it speeds up the compilation process, but the disadvantage is that it loses some freedom, makes it difficult to optimize, and makes the compiler logic complex. Specifically, there is a wiki on the official PHP website that discusses the introduction of AST in the compilation process of PHP. Of course, it is definitely very difficult to implement.
We usually don’t come into contact with the compilation process of PHP, but AST is a useful thing. Many tools in our daily work include AST, such as PHP_CodeSniffer, PHP_Depend, ZendStudio, and PDT. The source code needs to be When performing analysis, the help of AST is more or less required.
Let’s take a look at the PHP7 and php5 compilation process, as shown in the picture
The above has introduced the PHP parsing process, including aspects of the process. I hope it will be helpful to friends who are interested in PHP tutorials.