As a commonly used programming language, PHP is widely used in Web development. When writing PHP code, it ultimately needs to be executed on the server side. Before PHP executes code, it needs to go through four steps: lexical analysis, syntax analysis, compilation and execution. This article will introduce these four steps in detail.
1. Lexical analysis
Lexical analysis is the process of decomposing code into lexical units (tokens). In PHP, each type of lexical unit is defined by the language specification. These vocabulary units include keywords, variables, functions, symbols, etc. In PHP, lexical analysis is done by the lexical analyzer. A lexer is a program that receives PHP code as input and breaks it into words according to defined rules.
For example, in the following code, the lexical analyzer will decompose it into 7 vocabulary units:
for ($i = 0; $i < 10; $i++) { echo $i; }
These vocabulary units includefor
,(
、$i
、=
、0
、;
etc.
2. Syntax analysis
Grammar analysis is the process of converting lexical units after lexical analysis into a syntax tree (parse tree). The syntax tree is an abstract syntax structure, which reflects the syntax structure of the code. Syntax analysis is also composed of the corresponding This is done by the program, that is, the syntax analyzer. The syntax analyzer will check the syntax rules in the code and generate the corresponding syntax tree.
For example, in the following code, the syntax analyzer will generate afor
Looped syntax tree:
for ($i = 0; $i < 10; $i++) { echo $i; }
3. Compilation
Compilation is the process of converting the syntax tree into binary code. In PHP, this step is performed by the Zend engine Completed. The Zend engine is the core component of PHP. It will further optimize the syntax tree and convert it into bytecode.
Bytecode is an intermediate code, it is not like the source The code is as easy to read, but because it has been optimized and transformed, it executes faster. The compiled bytecode can be saved for later use.
4. Execution
Execution is the process of converting compiled bytecode into machine code and running it. In PHP, this step is completed by the virtual machine of the Zend engine. The virtual machine reads the bytecode and performs corresponding operations. In During the process of reading bytecode, the virtual machine executes instructions one by one and saves the results in memory.
For example, in the following code, the virtual machine executes three instructions in sequence andhello world!
Output to the screen:
echo "hello"; echo " "; echo "world!";
Summary
The four steps for PHP to execute code are: lexical analysis, syntax analysis, compilation and execution. These four The steps are completed by different programs and engines, working together to ultimately achieve the execution of the PHP code. It is very important for PHP developers to understand these steps, which helps them write more efficient and optimized code .At the same time, these steps also provide a foundation for learning some advanced PHP technologies, such as optimization, debugging, and security.
The above is the detailed content of How to execute code in php? Four steps analysis. For more information, please follow other related articles on the PHP Chinese website!