Let's start with the underlying working principles of PHP_PHP Tutorial

WBOY
Release: 2016-07-13 10:28:46
Original
898 people have browsed it

I have done .net and java development before, and I have also written several Php websites. It seems that I have been exposed to all three major programming languages. But more and more I feel that I lack an overall understanding of the entire programming process, especially the underlying mechanism. For example, network programming, compilation principles, server side, database storage engine principles, etc. So I read some books, the more classic ones include apue, unp, tcp/ip, nginx, mysql's innodb storage engine, and have a deep understanding of jvm. Gradually I discovered that no matter what language is used for development, there are linux, shell, c/c++, nginx server, and mysql behind it. Perhaps only by mastering these core principles can a programmer have core competitiveness.

The back-end part of BAT is inseparable from these core technologies, but the front-end (business logic layer) will be different. For example, Taobao mainly uses Java, and Baidu mainly uses PHP. Tencent is a tool-controlled group and mainly uses c/c++ technology. Tencent's main products are various clients (qq, input method, music...the most important thing is games) and servers under Windows. Relatively speaking, there are relatively few web products (QQ Space, Friends Network, etc.). These web products are relatively mature, and they only occasionally make improvements. Unless new products emerge, there will not be a large demand for talent.

Although the demand for talents in the fields of machine learning and big data mining seems to be relatively strong at present, related technologies still need to be built on Linux and JVM. The demand for Java talents in some companies will further increase.

Now that I understand the process of C language compilation and connection, and the running mechanism of Java under JVM, I am suddenly curious about the running process, mechanism and principle of PHP. I found a few blogs and got a rough idea. Put it below first:

The underlying working principle of PHP

Introduction
Let’s take a look at the process below:

    We have never manually started the PHP related process, it runs with the startup of Apache; PHP is connected to Apache through the mod_php5.so module (specifically, SAPI, the server application programming interface); PHP has a total of three Modules: kernel, Zend engine, and extension layer; PHP kernel is used to process requests, file streams, error handling and other related operations; Zend engine (ZE) is used to convert source files into machine language and then run it on a virtual machine; The extension layer is a set of functions, libraries, and streams that PHP uses to perform specific operations. For example, we need the mysql extension to connect to the MySQL database; when ZE executes the program, it may need to connect to several extensions. At this time, ZE hands over the control to the extension and returns it after processing the specific task; finally, ZE returns the program running results. to the PHP kernel, which then transmits the results to the SAPI layer and finally outputs them to the browser.

    In-depth discussion
    Wait, it’s not that simple. The above process is just a simplified version, let’s dig a little deeper to see what else is going on behind the scenes.

      After Apache is started, the PHP interpreter is also started; the PHP startup process has two steps; the first step is to initialize some environment variables, which will take effect throughout the SAPI life cycle; the second step is to generate only the current request Some variables are set.

      The first step to start PHP
      Not sure what the first and second steps are? Don’t worry, we’ll discuss this in detail next. Let's look at the first and most important step first. The thing to remember is that the first step of the operation happens before any requests arrive.

        After starting Apache, the PHP interpreter also starts; PHP calls the MINIT method of each extension to switch these extensions to an available state. Take a look at what extensions are opened in the php.ini file; MINIT means "module initialization". Each module defines a set of functions, class libraries, etc. to handle other requests.

        A typical MINIT method is as follows:
        PHP_MINIT_FUNCTION(extension_name){
        /* Initialize functions, classes etc */
        }
        The second step of PHP startup

          When a page request occurs, the SAPI layer hands over control to the PHP layer. So PHP sets the environment variables needed to reply to this request. At the same time, it also creates a variable table to store variable names and values ​​generated during execution. PHP calls the RINIT method of each module, which is "request initialization". A classic example is the RINIT of the Session module. If the Session module is enabled in php.ini, the $_SESSION variable will be initialized and the relevant content will be read in when the RINIT of the module is called; the RINIT method can be regarded as a The preparation process starts automatically between program executions.

          A typical RINIT method is as follows:
          PHP_RINIT_FUNCTION(extension_name) {
          /* Initialize session variables, pre-populate variables, redefine global variables etc */
          }
          The first step to close PHP
          Just like PHP startup, PHP shutdown is also divided into two steps:

            Once the page is executed (whether it reaches the end of the file or is terminated with the exit or die function), PHP will start the cleanup process. It will call the RSHUTDOWN method of each module in sequence. RSHUTDOWN is used to clear the symbol table generated when the program is running, that is, to call the unset function on each variable.

            A typical RSHUTDOWN method is as follows:
            PHP_RSHUTDOWN_FUNCTION(extension_name) {
            /* Do memory management, unset all variables used in the last PHP call etc */
            }
            The second step of PHP shutdown
            Finally, all requests have been processed, SAPI is ready to be closed, and PHP begins to execute the second step:

              PHP calls the MSHUTDOWN method of each extension, which is the last chance for each module to release memory.

              A typical RSHUTDOWN method is as follows:
              PHP_MSHUTDOWN_FUNCTION(extension_name) {
              /* Free handlers and persistent memory etc */
              }
              In this way, the entire PHP life cycle is over. It should be noted that the "starting step one" and "closing step two" will only be executed if there is no request from the server.

              The following is illustrated with some diagrams!

              The underlying working principle of PHP

              Lets start with the underlying working principles of PHP_PHP Tutorial

              Figure 1 PHP structure

              As can be seen from the picture, 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 (a series of compilation processes such as lexical and syntax analysis) into executable opcode processing and implements corresponding processing methods and implements basic data structures (such as hashtable, oo), memory allocation and management, and provides corresponding api methods for external calls. It is the core of everything. All peripheral functions are implemented around zend.

              ②Extensions

              Around the zend engine, extensions provide various basic services in a component-based manner. Our common various built-in functions (such as array series), standard libraries, etc. are all implemented through extensions. Users can also implement their own extensions as needed. To achieve functions expansion, performance optimization and other purposes (for example, the PHP middle layer and rich text parsing currently used by Tieba are typical applications of extension).

              ③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 a very elegant and successful design of PHP. Through sapi, PHP itself and The upper-layer application is decoupled and isolated. 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. It will be introduced later in the sapi chapter

              ④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, running them in script mode on the command line, etc.

              Architectural ideas:

              The engine (Zend) + component (ext) model reduces internal coupling

              The middle layer (sapi) isolates the web server and php

              *************************************************** ************************

              If php was a car, then

              The framework of the car is php itself

              Zend is the engine of the car

              The various components under Ext are the wheels of the car

              Sapi can be regarded as a road, and cars can run on different types of roads

              The execution of a PHP program is a car running on the road.

              Therefore, we need: excellent engine + suitable wheels + correct runway

              The relationship between Apache and php

              Apache's parsing of php is completed through the php Module among many Modules.

              Lets start with the underlying working principles of PHP_PHP Tutorial

              To finally integrate PHP into the Apache system, you need to make some necessary settings for Apache. Here, we will take the mod_php5 SAPI operating mode of php as an example to explain. As for the concept of SAPI, we will explain it in detail later.

              Assuming that the versions we installed are Apache2 and Php5, then we need to edit Apache’s main configuration file http.conf and add the following lines to it:

              In Unix/Linux environment:

              LoadModule php5_module modules/mod_php5.so

              AddType application/x-httpd-php .php

              Note: modules/mod_php5.so is the installation location of the mod_php5.so file in the X system environment.

              In Windows environment:

              LoadModule php5_module d:/php/php5apache2.dll

              AddType application/x-httpd-php .php

              Note: d:/php/php5apache2.dll is the installation location of the php5apache2.dll file in the Windows environment.

              These two configurations tell Apache Server that any URL user requests received in the future with php as the suffix will need to call the php5_module module (mod_php5.so/php5apache2.dll) for processing.

              Apache life cycle

              Apach’s request processing process

              Detailed explanation of Apache request processing loop
              What do the 11 stages of the Apache request processing cycle do? (Are these 11 stages the corresponding 11 processing stages in nginx???) 喎?http://www.Bkjia.com/kf/ware/vc/" target="_blank" class="keylink">vcD4KPHA CjGholBvc3QtUmVhZC1SZXF1ZXN0vde2zjwvcD4KPHA CiAgICDU2tX9s6PH68fztKbA7cH3s8zW0KOs1eLKx8Sjv m/ydLUsuXI67 mz19O1xLXa0ru49r3Xts6ho7bU09rEx9Cpz u63NTnvfjI67SmwO3H68fztcTEo7/pwLTLtaOs1eK49r3Xts6/ydLUsbvA 9PDoaM8L3A CjxwPgogICAgMqGiVVJJIFRyYW5zbGF0aW9uv de2ziA8YnI CiAgICBBcGFjaGXU2rG vde2zrXE1vfSqrmk1/ejur2rx vH87XEVVJM07PJ5LW9sb612M7EvP7Ptc2zoaPEo7/pv8nS1NTa1eK917bOsuXI67mz19OjrNa00NDX1Ly6tcTTs8nk wt 8raGjbW9kX2FsaWFzvs3Kx8D708PV4rj2vde2zrmk1/e1xKGjPC9wPgo8cD4KICAgIDOhokhlYWRlciBQYXJzaW5nvde2ziA8YnI CiAgICBBcGFjaGXU2rG vde2zrXE1vfSqrmk1/ejurzssunH68 fztcTNt7K/ oaPTydPaxKO/6b/J0tTU2sfrx/O0psDtwfezzLXEyM66ztK7uPa148nP1rTQ0LzssunH68fzzbeyv7XEyM7O8aOs0vK0y9XiuPa5s9fTutzJ2bG7yrnTw6GjbW9kX3NldGVudmlmvs3Kx8D 708PV4rj2vde2zrmk1/e1xKGjPC9wPgo8cD4KICAgIDShokFjY2VzcyBDb250cm9svde2ziA8YnI CiAgICBBcGFjaGXU2rG vde2zrXE1vfSqrmk1/ejurj5vt3F5NbDzsS8/rzssunKx7fx1MrQ7b fDzsrH68fztcTXytS0oaNBcGFjaGW1xLHq17zC37ytyrXP1sHL1MrQ7brNvty Na4we6ho21vZF9hdXRoel9ob3N0vs3Kx8D708PV4rj2vde2zrmk1/e1xKGjPC9wPgo8cD4KICAgIDWhokF1dGhlbnRp 8f40/Kho8Sjv m/ ydLU1NrV4r3Xts6y5cjrubPX06OsyrXP1tK7uPbIz9akt723qKGjPC9wPgo8cD4KICAgIDaho kF1dGhvcml6YXRpb26917bOIDxicj4KICAgIEFwYWNoZdTasb6917bOtcTW99KquaTX96O6uP m 3cXk1sPOxLz vOyy6crHt/HUytDtyM/WpLn9tcTTw7un1rTQ0Mfrx/O1xLLZ1/eho8Sjv m/ydLU1NrV4r3Xts6y5cjrubPX06OsyrXP1tK7uPbTw7unyKjP3rncwO21xLe9t6ihozw vcD4KPHA CiAgICA3oaJNSU1FIFR5cGUgQ2hlY2tpbme917bOIDxicj4KICAgIEFwYWNoZdTasb6917bOtcTW99KquaTX96O6uPm 3cfrx/PXytS0tcRNSU1FwODQzbXEz C52Lnm1PKjrMXQtqi9q9Kqy rnTw7XExNrI3bSmwO26r8r9oaOx6te8xKO/6W1vZF9uZWdvdGlhdGlvbrrNbW9kX21pbWXKtc/WwcvV4rj2ubPX06GjPC9wPgo8cD4KICAgIDihokZpeFVwvde2ziA8YnI CiAgICDV4sr H0ru49s2o08O1xL3Xts6jrNTK0O3Eo7/p1NrE2sjdyfqzycb31q7HsKOs1MvQ0MjOus6x2NKqtcS0psDtwfezzKGjus1Qb3N0X1JlYWRfUmVxdWVzdMDgJiMyMDI4NDujrNXiysfSu7j2xN y5u7K2u /HIzrrO0MXPorXEubPX06Os0rLKx9fus6PKudPDtcS5s9fToaM8L3A CjxwPgogICAgOaGiUmVzcG9uc2W917bOIDxicj4KICAgIEFwYWNoZdTasb6917bOtcTW99KquaTX96O6yfqzybe1u9i/zbuntsu1x MTayN2jrLi61PC4 L/Nu6e2y7eiy83Su7j2x6G1sbXEu9i4tKGj1eK49r3Xts7Kx9X7uPa0psDtwfezzLXEusvQxLK/t9ahozwvcD4KPHA CiAgICAxMKGiTG9nZ2luZ73Xts4gPGJyPgogICAgQXBhY 2hl1Nqxvr3Xts61xNb30qq5pNf3o7rU2rvYuLTS0b6tt6LLzbj4v827p7bL1q6687zHw rzKws7xoaPEo7/pv8nE3NDeuMS78tXfzOa7u0FwYWNoZbXEserXvMjV1r68x8K8oaM8L 3 A 87EvP6hosS/wry1xLSmwO278tXfU29ja2V0tcS52LHVtci1yKOs1eLKx0FwYWNoZdK7tM7H68fztKbA7bXE1 6689K7uPa917bOoaM8L3A CjxwPgo8c3Ryb25nPkxBTVC83Lm5o7o8 L3N0cm9uZz48L3A ​​CjxwPgo8aW1nIHNyYz0="http://www.2cto.com/uploadfile/Collfiles/20140607/20140607091102327.gif" alt="  b簗 ﹊萟?钅裣i啔a 篧综合?{鷌?*??sNa拉?'瞝_椐鷌rough铻 Yu?凓i僃i?耉彦y? m4簖html

              Baidu R&D Center’s blog http://stblog.baidu-tech.com/?p=763

              Wang Xingbin’s blog http://blog.csdn.net/wanghao72214/article/details/3916825

              www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/780964.htmlTechArticleI have done .net, java development before, and also written several Php websites, almost 3 major programming languages All contacted. But I feel more and more that I lack an overall understanding of the entire programming process...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template