search
HomeBackend DevelopmentPHP TutorialDetailed explanation of the underlying operating mechanism and working principles of PHP

Detailed explanation of the underlying operating mechanism and working principles of PHP

I recently set up a server, and suddenly I felt how the lamps work, or how they are connected? I usually just write programs, and I have never thought about the working principle between them:

The underlying working principle of PHP

Related learning recommendations: PHP programming from entry to proficiency

Figure 1 PHP structure

As can be seen from the picture, php It is a 4-layer system from bottom to top

①Zend engine

Zend is implemented in pure C and is the core part of PHP. It translates PHP code (lexical, syntax analysis, etc.) Process) processes executable opcodes and implements corresponding processing methods, 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 through extensions To achieve this, 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 allows PHP to interact with peripheral data through a series of hook functions. This is PHP's very elegant and A successful design successfully decouples and isolates PHP itself 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. We will introduce it 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 through webserver Apply, run as a script from 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 is a car, then
the framework of the car It is php itself
Zend is the engine (engine) of the car
The various components below Ext are the wheels of the car
Sapi can be regarded as a road, and the car can run on different types of roads
The execution of a php program is like a car running on the road.
Therefore, we need: Excellent performance engine, suitable wheels, correct runway

The relationship between Apache and php

Apache for php The analysis is completed through the php Module among many Modules.

#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.

Assume that the versions we install are Apache2 and Php5, then you 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 Windows environment.

These two configurations tell Apache Server that any 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

##Apache’s request processing process

Detailed explanation of the Apache request processing cycle

The 11 stages of the Apache request processing cycle have been completed What things?

1. Post-Read-Request phase

In the normal request processing process, this is the first stage in which the module can insert hooks. This stage can be exploited for modules that want to get into processing requests very early.

2. URI Translation stage

Apache’s main job in this stage: mapping the requested URL to the local file system. Modules can insert hooks at this stage to execute their own mapping logic. mod_alias uses this phase to work.

3. Header Parsing stage
Apache’s main work in this stage: check the header of the request. Since the module can perform the task of checking request headers at any point in the request processing flow, this hook is rarely used. mod_setenvif uses this phase to work.

4. Access Control Phase
The main work of Apache in this phase: Check whether access to the requested resources is allowed according to the configuration file. Apache's standard logic implements allow and deny directives. mod_authz_host uses this phase to work.

5. Authentication stage
The main work of Apache in this stage: authenticate users according to the policy set in the configuration file, and set the user name area. Modules can insert hooks at this stage to implement an authentication method.

6. Authorization phase
The main work of Apache in this phase: Check whether authenticated users are allowed to perform the requested operation according to the configuration file. The module can insert hooks at this stage to implement a user rights management method.

7. MIME Type Checking stage
The main work of Apache in this stage: determine the content processing function to be used according to the relevant rules of the MIME type of the requested resource. The standard modules mod_negotiation and mod_mime implement this hook.

8. FixUp stage
This is a general stage that allows the module to run any necessary processing before the content generator. Similar to Post_Read_Request, this is a hook that can capture any information and is also the most commonly used hook.

9. Response stage
Apache’s main job in this stage is to generate content returned to the client and be responsible for sending an appropriate reply to the client. This stage is the core part of the entire process.

10. Logging phase
The main work of Apache in this phase: recording transactions after the reply has been sent to the client. Modules may modify or replace Apache's standard logging.

11. CleanUp Phase
The main work of Apache in this phase: clean up the environment left after the completion of this request transaction, such as the processing of files and directories or the closing of Socket, etc. This is the first time for Apache The final stage of request processing.

LAMP architecture:

##Four layers from bottom to top:

①liunx Belongs to the bottom layer of the operating system

②apache server, belongs to the secondary server, communicates with Linux and PHP

③php: belongs to the server-side programming language, and is associated with apache through the php_module module

④mysql and Other web services: belong to application services and are associated with mysql through PHP's Extensions plug-in module

Android system architecture diagram

Lamp and Android architecture Comparing the pictures, it seems to be somewhat similar to the lamp architecture. I don't know Android, but it feels a bit similar. Experts can point out the differences. I would be very grateful.

From top to bottom Next:

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

1. Application----- ---Specific application--------web application

2. Application framework----java-------------PHP language and library

3. System runtime library:----Virtual machine---------WEB server

⒋Linux kernel:---Operating system-------Lamp architecture The relationship between the L

lamp and the inside of the computer

The CPU is the factory, the hard disk is the large warehouse, and the memory is the regular transfer center , virtual memory is a temporary transfer center

Php language is compiled into machine language by zend, operating cpu

The operation of the database is an I/O operation and a mechanical movement, that is to say, the operation of a website The bottleneck is caused by the reading and writing of the hard disk. The solution is to reduce the number of I/O operations and use buffering technology. That is, the data operations are placed in the mencache. When it reaches a certain order of magnitude, it is written to the database at once. The mencache belongs to the key. --value relationship

Non-relational data is also built based on this concept, and also belongs to key--value relationship

Frequent read operations------Put it in mencache

Read more and write less----Put it in nosql------The reading function is very powerful!

The above is the detailed content of Detailed explanation of the underlying operating mechanism and working principles of PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:jb51. If there is any infringement, please contact admin@php.cn delete
PHP in Action: Real-World Examples and ApplicationsPHP in Action: Real-World Examples and ApplicationsApr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP: Creating Interactive Web Content with EasePHP: Creating Interactive Web Content with EaseApr 14, 2025 am 12:15 AM

PHP makes it easy to create interactive web content. 1) Dynamically generate content by embedding HTML and display it in real time based on user input or database data. 2) Process form submission and generate dynamic output to ensure that htmlspecialchars is used to prevent XSS. 3) Use MySQL to create a user registration system, and use password_hash and preprocessing statements to enhance security. Mastering these techniques will improve the efficiency of web development.

PHP and Python: Comparing Two Popular Programming LanguagesPHP and Python: Comparing Two Popular Programming LanguagesApr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

The Enduring Relevance of PHP: Is It Still Alive?The Enduring Relevance of PHP: Is It Still Alive?Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP's Current Status: A Look at Web Development TrendsPHP's Current Status: A Look at Web Development TrendsApr 13, 2025 am 12:20 AM

PHP remains important in modern web development, especially in content management and e-commerce platforms. 1) PHP has a rich ecosystem and strong framework support, such as Laravel and Symfony. 2) Performance optimization can be achieved through OPcache and Nginx. 3) PHP8.0 introduces JIT compiler to improve performance. 4) Cloud-native applications are deployed through Docker and Kubernetes to improve flexibility and scalability.

PHP vs. Other Languages: A ComparisonPHP vs. Other Languages: A ComparisonApr 13, 2025 am 12:19 AM

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP vs. Python: Core Features and FunctionalityPHP vs. Python: Core Features and FunctionalityApr 13, 2025 am 12:16 AM

PHP and Python each have their own advantages and are suitable for different scenarios. 1.PHP is suitable for web development and provides built-in web servers and rich function libraries. 2. Python is suitable for data science and machine learning, with concise syntax and a powerful standard library. When choosing, it should be decided based on project requirements.

PHP: A Key Language for Web DevelopmentPHP: A Key Language for Web DevelopmentApr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software