Home  >  Article  >  Backend Development  >  An in-depth analysis of the underlying running process of PHP

An in-depth analysis of the underlying running process of PHP

青灯夜游
青灯夜游forward
2021-06-10 14:39:332326browse

This article will give you an in-depth analysis of the PHP Running process. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.

An in-depth analysis of the underlying running process of PHP

#The underlying language of PHP is C language. C language is a compiled language.

Compiled language: The program requires a special compilation process before execution. The program is compiled into a machine language file. There is no need to re-translate when running. Just use the compiled results directly. . The program execution efficiency is high, it depends on the compiler, and its cross-platform performance is poor. How did it become an interpreted language?

I used processon to make a picture, and then I will continue to fill in the holes slowly

If the picture is not clear, you can see the original picture

An in-depth analysis of the underlying running process of PHPAddress: http://assets.processon.com/chart_image/603b293b1e08530bb1adcb1c.png

Briefly explain the process of the picture

First, do not consider the CLI and FPM processes C The language is parsed to the file with php suffix through the open_file_sanning function

2 Call the compilation function zend_compile_file() to compile. The specific implementation of this function actually includes two main processes: lexical analysis (Lex implementation) and syntax analysis (Yacc implementation). After executing this function: the compilation of the php script is complete. The input of this function is: php script file, and the output is op_array. To put it simply: the compilation process is to parse the script into instructions that the php virtual machine can process, and op_array is just an array made of these instructions ( This is very similar to the assembly code generated by compilation of some compiled languages. It is also a series of commands.

3 The VM virtual machine calls the zend_execute() function to execute. The input of this function is the op_array generated in the compilation stage above. Here he will parse each command and process it. There are nearly 200 op commands in PHP7.4.5, so it needs to process these commands. Each command has a corresponding processor for processing. The zend virtual virtual machine will be based on The type of each command in op_array is distributed to the corresponding processor for processing.

The processor here is the processor of the zend virtual machine. It is not the CPU

Zend virtual machine processor actually It is the logic for processing the op command. It can exist in the form of a function or a logical segment, depending on the command distribution method

Zend There are three mechanisms for distributing commands in the virtual machine: CALL , SWITCH, and GOTO are three types. Both switch and goto have corresponding logic segments in the zend_execute() function, which can be executed directly. And call executes function calls in the zend_execute() function.

You can find that the PHP code actually runs in the zend vm virtual machine and does not directly run in the CPU

The emergence of JIT in PHP8 can allow part of the code to be compiled into machine code Running in the CPU For this part, you can refer to the analysis done by @rhett's article "PHP JIT Technology Detailed Explanation". Article address: https://zhuanlan.zhihu.com/p/331777202

Thanks to senior Chen Lei for his "PHP7 Source code underlying design and implementation》

This article has been approved by the original author PHP Cui Xuefeng and published on the PHP Chinese website. The original address is: https://zhuanlan.zhihu.com/p/353501903


Recommended learning: "
PHP Video Tutorial

"

The above is the detailed content of An in-depth analysis of the underlying running process of PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:zhihu.com. If there is any infringement, please contact admin@php.cn delete