Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the underlying working principle of PHP?

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article will explain in detail what the underlying working principle of PHP is, Xiaobian thinks it is quite practical, so share it with you as a reference, I hope you can gain something after reading this article.

How PHP works at the bottom

Figure 1 Php structure

As you can see from the diagram above, php is a 4-layer system from bottom to top.

1 Zend engine

Zend is implemented in pure C as a whole. It is the kernel part of PHP. It translates PHP code (lexical, syntax parsing and other compilation processes) into executable opcode processing and implements corresponding processing methods. It implements basic data structures (such as hashtable and 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 built-in functions (such as array series) and standard libraries are implemented through extensions, and users can also implement their own extensions according to their needs to achieve functional expansion and performance optimization (such as the php middle layer being used in Tieba, rich text parsing is a typical application of extensions).

③Sapi

Sapi is the full name of the Server Application Programming Interface, that is, the server-side application programming interface, sapi through a series of hook functions, so that php can interact with peripheral data, this is php very elegant and successful design, through sapi successfully decoupled php itself and the upper application isolation, php can no longer consider how to be compatible for different applications, and the application itself can also achieve different processing methods for its own characteristics. This will be described later in the sapi chapter

④ Upper application

This is the php program we usually write, through different sapi ways to get a variety of application patterns, such as web application through webserver, script mode under the command line, and so on.

Framing ideas:

Engine (Zend)+ Component (ext) pattern reduces internal coupling

The middle layer (sapi) isolates web servers from php

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

If PHP is a car, then

The car frame is PHP itself.

Zend is the engine of the car.

The various components below Ext are the wheels of the car.

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

And a php execution is a car running on the road.

Therefore, we need: excellent performance engine + the right wheels + the right track

Relationship between Apache and PHP

Apache's parsing of php is done through php modules in many modules.

Finally integrating php into Apache requires some necessary Apache setup. Here, we will take php mod_php5 SAPI operation mode as an example to explain, as for SAPI this concept we will explain in detail later.

Assuming we have Apache2 and Php5 installed, we need to edit Apache's main configuration file http.conf to add the following lines:

In Unix/Linux environments:

LoadModule php5_module modules/mod_php5.so

AddType application/x-httpd-php .php

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

Under Windows:

LoadModule php5_module d:/php/php5apache2.dll

AddType application/x-httpd-php .php

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

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

Apache's life cycle

Apach's request processing flow

Apache request processing loop details

What do the 11 stages of the Apache request processing loop do?

Post-Read-Request phase

In the normal request processing flow, these are the *** stages at which modules can insert hooks. This phase can be exploited for modules that want to get into processing requests early.

2. URI Translation Stage

Apache's main job at this stage is to map the requested URL to the local file system. Modules can insert hooks at this stage and perform their own mapping logic. mod_alias is used to work at this stage.

3. Header Parsing Phase

Apache's main work at this stage: checking the header of the request. This hook is rarely used because the module can perform the task of inspecting the request header at any point in the request processing flow. mod_setenvif is used to work at this stage.

4. Access Control phase

Apache's main work at this stage: checking whether access to the requested resource is allowed based on the configuration file. Apache's standard logic implements allow and deny directives. mod_authz_host is what works at this stage.

*** The authentication phase

Apache's main work at this stage: authenticate users according to the policies set by the configuration file and set the username area. Modules can insert hooks at this stage to implement an authentication method.

6. Authorization phase

Apache's main task at this stage is to check whether authenticated users are allowed to perform the requested action based on the configuration file. Modules can insert hooks at this stage to implement a method of user rights management.

MIME Type Checking phase

Apache's main task at this stage is to determine which content processing function to use based on rules related to the MIME type of the requested resource. The standard modules mod_negotiation and mod_mime implement this hook.

8. Fixup phase

This is a generic phase that allows modules to run any necessary processing before the content generator. Similar to Post_Read_Request, this is a hook that captures any information and is the most commonly used hook.

9. Response phase

Apache's main job at this stage is to generate the content that is returned to the client and is responsible for sending an appropriate reply to the client. This phase is the core of the entire process.

10. Logging phase

Apache's main job at this stage is to log the transaction after the reply has been sent to the client. Modules may modify or replace Apache's standard logging.

11. CleanUp phase

Apache's main work at this stage: cleaning up the environment left behind after the completion of this request transaction, such as file, directory processing or socket closure, etc., this is Apache's first request processing stage.

LAMP Architecture:

Four levels up:

①Liunx belongs to the bottom layer of the operating system

②Apache server, belonging to the secondary server, communication linux and PHP

③php: a server-side programming language, associated with apache through php_module

④mysql and other web services: application services, associated with mysql through PHP Extensions

Android System Architecture

lamp and android architecture comparison, seems to be a bit similar to lamp architecture, I do not understand android, just feel a bit similar, experts can point out the difference, little brother here thanks

From top to bottom:

Android Architecture------------Description-----LAMP Architecture

1. Applications-----Specific Applications-----Web Applications

2. Application Framework---java-------PHP Language and Libraries

3. System Runtime: ---Virtual Machine-----WEB Server

Linux kernel: ---Operating system-----L in lamp architecture

About "PHP underlying working principle is what" this article is shared here, I hope the above content can be of some help to everyone, so that you can learn more knowledge, if you think the article is good, please share it to let more people see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report