


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
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.
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:
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.lamp and the inside of the computer
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

In this chapter, we are going to learn the following topics related to routing ?

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

Validator can be created by adding the following two lines in the controller.
