Home >Backend Development >PHP Tutorial >Detailed explanation of the underlying operating mechanism of PHP
In this article, we mainly share with you a detailed explanation of the underlying operating mechanism of PHP. First, we will share with you the design concepts and characteristics of PHP, the four-layer system of PHP, etc. We hope it can help you.
Multi-process model: Since PHP is a multi-process model, different requests do not interfere with each other, which ensures that one request will hang The loss will not affect the overall service. Of course, with the development of the times, PHP has already supported the multi-threading model.
Weakly typed language: Unlike C/C++, Java, C# and other languages, PHP is a weakly typed language. The type of a variable is not determined at the beginning. It is determined during operation and implicit or explicit type conversion may occur. The flexibility of this mechanism is very convenient and efficient in web development. The details will be discussed in PHP later. Variables are detailed in.
Interpreted language: PHP is different from C/C++, Java, C# and other compiled languages in the running steps. PHP needs to be parsed into a compiled language through lexical and syntactic analysis first. , to run! Therefore, PHP is not suitable for large-scale applications such as high performance or big data calculations. Although there is no difference between 0.001 seconds and 0.1 seconds for browser users, it is not suitable for other fields
The engine (Zend) + component (ext) mode reduces internal coupling.
The middle layer (sapi) isolates the web server and PHP.
The syntax is simple and flexible, without too many specifications. Shortcomings lead to mixed styles, but no matter how bad a programmer is, he will not write a program that is too outrageous and endangers the overall situation.
The core architecture of PHP is as shown below:
From the picture It can be seen that PHP is a 4-layer system from bottom to top:
Zend engine: Zend is implemented entirely in pure C and is the core part of PHP. It translates PHP code (lexical , grammar parsing and a series of compilation processes) to process executable opcodes and implement corresponding processing methods, implement basic data structures (such as hashtable, oo), memory allocation and management, and provide corresponding api methods for external calls. It is the core of everything, and all peripheral functions are implemented around Zend.
Extensions: Around the Zend engine, extensions provide various basic services in a component-based manner. Our common built-in functions (such as array series), standard libraries, etc. are all passed through extension, users can also implement their own extensions as needed to achieve function expansion, performance optimization and other purposes (for example, the PHP middle layer and rich text parsing currently used by Tieba are typical applications of extensions).
Sapi: The full name of Sapi is Server Application Programming Interface, which is the server application programming interface. Sapi enables PHP to interact with peripheral data through a series of hook functions. This is very elegant for PHP. With a successful design, PHP itself has been successfully decoupled and isolated from upper-layer applications through sapi. PHP can no longer consider how to be compatible with different applications, and the application itself can also implement different processing methods according to its own characteristics.
Upper-layer application: This is the PHP program we usually write. We can obtain various application modes through different sapi methods, such as implementing web applications through webserver, and using Run in script mode, etc.
If PHP is a car, then the framework of the car is PHP itself, Zend is the engine (engine) of the car, and the various components under Ext are the wheels of the car. Sapi can see It is a road, and cars can run on different types of roads, and the execution of a PHP program means that the car runs on the road. Therefore, we need: a high-performance engine + the right wheels + the right track.
As mentioned above, Sapi allows external applications to exchange data with PHP through a series of interfaces and implement specific processing methods according to different application characteristics. We commonly see Some of the sapis are:
apache2handler: This is the processing method when using apache as the webserver and running in mod_PHP mode. It is also the most widely used one now.
cgi: This is another direct interaction method between webserver and PHP, which is the famous fastcgi protocol. In recent years, fastcgi+PHP has been used more and more, and it is also asynchronous. The only method supported by webserver.
cli: Application mode for command line calls
Let’s take a look first The process through which PHP code is executed.
As you can see from the picture, PHP implements a typical dynamic language execution process: after getting a piece of code, after lexical analysis, syntax analysis and other stages, the source program will be translated into instructions (opcodes). The ZEND virtual machine then executes these instructions in sequence to complete the operation. PHP itself is implemented in C, so the functions ultimately called are all C functions. In fact, we can regard PHP as a software developed in C.
The core of PHP execution is the translated instructions, that is, opcode.
Opcode is the most basic unit of PHP program execution. An opcode consists of two parameters (op1, op2), return value and processing function. The PHP program is ultimately translated into the sequential execution of a set of opcode processing functions.
Several common processing functions:
PHP
##123456 | ##ZEND_ASSIGN_SPEC_CV_CV_HANDLER : Variable allocation ($a=$b) ZEND_DO_FCALL_BY_NAME_SPEC_HANDLER: Function call ZEND_CONCAT_SPEC_CV_CV_HANDLER: String concatenation $a.$b ZEND_ADD_SPEC_CV_CONST_HANDLER: Addition $a+2 ZEND_IS_EQUAL_SPEC_CV_CONST: Judge equality $a== 1 ZEND_IS_IDENTICAL_SPEC_CV_CONST: Judgment equal $a===1 |
1 2 3 4 5 6 7 8 9 10 |
##getKeyHashValueh;index=n&nTableMask ;Bucket*p=arBucket[index];while(p){if((p->h==h)&(p-> ;nKeyLength==nKeyLength)){RETURNp->data; }p=p->next;}RETURNFALTURE; |
2 3 4 5 | IS_LONG -> lvalue
IS_DOUBLE -> dvalue IS_ARRAY -> ht IS_STRING -> str IS_RESOURCE -> lvalue |
1 2 3 4 5 6 7 8 |
##$res=$strA.$strB and $res=“$strA$strB” In this case, zend will malloc a piece of memory again and process it accordingly. The speed is generally $strA=$strA.$strBThis is the fastest. zend will directly relloc based on the current strA to avoid repeated copying $res=$intA.$intB This is slower because it requires implicit format conversion and the actual writing In the program, you should also pay attention to avoid $strA=sprintf(“%s%s”,$strA.$strB);This will be the slowest way, because sprintf PHP is not a language structure. It takes a lot of time to identify and process the format. In addition, the mechanism itself is malloc. However, the sprintf method is the most readable, and in practice it can be chosen flexibly according to specific circumstances. |
The above is the detailed content of Detailed explanation of the underlying operating mechanism of PHP. For more information, please follow other related articles on the PHP Chinese website!