search
HomeBackend DevelopmentPHP TutorialThe basics of how PHP works

The basics of how PHP works

Jul 04, 2018 pm 04:43 PM

This article mainly introduces the basics of the working principle of PHP. It has a certain reference value. Now I share it with everyone. Friends in need can refer to it.

I recently built a server and suddenly felt like a lamp. How exactly does it work, or how is it connected? I usually just write programs, and I have never thought about the working principle between them:

The underlying working principle of PHP

Figure 1 PHP structure

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

①Zend engine

Zend as a whole is implemented in pure C and is the core part of PHP. It combines PHP code with Translation (a series of compilation processes such as lexical and syntax analysis) 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 are 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 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 was a car, then

The framework of the car is php itself

Zend is the engine of the car

The various components below Ext are the wheels of the car

Sapi can be regarded as a road, a car It can run on different types of roads

And the execution of a php program means the car runs on the road.

Therefore, we need: an engine with excellent performance, a suitable wheel, a correct runway

The relationship between Apache and php

Apache’s analysis of php, It is accomplished 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 mod_php5.so in the X system environment The location where the file is installed.

In Windows environment:

LoadModule php5_module d:/php/php5apache2.dll

AddType application/x-httpd-php .php

Note: Among them, 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 need to call the php5_module module (mod_php5.so/php5apache2.dll) for processing.

Apache’s life cycle

Apache’s request processing process

Apache request Detailed explanation of processing loop
What are done in the 11 stages of Apache request processing loop?

1. Post-Read-Request stage

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

2. URI Translation Phase
The main work of Apache in this phase: 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 Phase
The main work of Apache in this phase: 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 resource 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 is to authenticate users according to the policies 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 stage
The main work of Apache in this stage is to 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 Phase
The main work of Apache in this phase is to determine the content processing function to be used based on 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
The main work of Apache 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 stage
The main work of Apache in this stage: recording the transaction 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 the application Service, associated with mysql through PHP's Extensions plug-in module

Android system architecture diagram

Compare lamp and Android's architecture diagram, it seems to be somewhat similar to lamp architecture, I I don’t understand Android, but it feels a bit similar. Experts can point out the differences. I would be very grateful.

From top to bottom:

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------L

in lamp architecture The relationship between the lamp and the inside of the computer

The CPU is the factory, the hard disk is the large warehouse, the memory is the regular transfer center, and the virtual memory is the 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. In other words, the bottleneck of a website is caused by reading and writing to the hard disk. The solution is to reduce the number of I/O operations. The use of buffering technology means that the data operations are placed in the mencache, and when it reaches a certain order of magnitude, it is written to the database at once. The mencache belongs to the key--value relationship

rather than relational data. It is also built based on this concept. , also belongs to the 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 entire content of this article. I hope it will be helpful to everyone's study. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

PHP source code differentiation platform MVC structure introduction

Analysis of Laravel5 quick authentication logic process

The above is the detailed content of The basics of how PHP works. For more information, please follow other related articles on the PHP Chinese website!

Statement
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
How do you modify data stored in a PHP session?How do you modify data stored in a PHP session?Apr 27, 2025 am 12:23 AM

TomodifydatainaPHPsession,startthesessionwithsession_start(),thenuse$_SESSIONtoset,modify,orremovevariables.1)Startthesession.2)Setormodifysessionvariablesusing$_SESSION.3)Removevariableswithunset().4)Clearallvariableswithsession_unset().5)Destroythe

Give an example of storing an array in a PHP session.Give an example of storing an array in a PHP session.Apr 27, 2025 am 12:20 AM

Arrays can be stored in PHP sessions. 1. Start the session and use session_start(). 2. Create an array and store it in $_SESSION. 3. Retrieve the array through $_SESSION. 4. Optimize session data to improve performance.

How does garbage collection work for PHP sessions?How does garbage collection work for PHP sessions?Apr 27, 2025 am 12:19 AM

PHP session garbage collection is triggered through a probability mechanism to clean up expired session data. 1) Set the trigger probability and session life cycle in the configuration file; 2) You can use cron tasks to optimize high-load applications; 3) You need to balance the garbage collection frequency and performance to avoid data loss.

How can you trace session activity in PHP?How can you trace session activity in PHP?Apr 27, 2025 am 12:10 AM

Tracking user session activities in PHP is implemented through session management. 1) Use session_start() to start the session. 2) Store and access data through the $_SESSION array. 3) Call session_destroy() to end the session. Session tracking is used for user behavior analysis, security monitoring, and performance optimization.

How can you use a database to store PHP session data?How can you use a database to store PHP session data?Apr 27, 2025 am 12:02 AM

Using databases to store PHP session data can improve performance and scalability. 1) Configure MySQL to store session data: Set up the session processor in php.ini or PHP code. 2) Implement custom session processor: define open, close, read, write and other functions to interact with the database. 3) Optimization and best practices: Use indexing, caching, data compression and distributed storage to improve performance.

Explain the concept of a PHP session in simple terms.Explain the concept of a PHP session in simple terms.Apr 26, 2025 am 12:09 AM

PHPsessionstrackuserdataacrossmultiplepagerequestsusingauniqueIDstoredinacookie.Here'showtomanagethemeffectively:1)Startasessionwithsession_start()andstoredatain$_SESSION.2)RegeneratethesessionIDafterloginwithsession_regenerate_id(true)topreventsessi

How do you loop through all the values stored in a PHP session?How do you loop through all the values stored in a PHP session?Apr 26, 2025 am 12:06 AM

In PHP, iterating through session data can be achieved through the following steps: 1. Start the session using session_start(). 2. Iterate through foreach loop through all key-value pairs in the $_SESSION array. 3. When processing complex data structures, use is_array() or is_object() functions and use print_r() to output detailed information. 4. When optimizing traversal, paging can be used to avoid processing large amounts of data at one time. This will help you manage and use PHP session data more efficiently in your actual project.

Explain how to use sessions for user authentication.Explain how to use sessions for user authentication.Apr 26, 2025 am 12:04 AM

The session realizes user authentication through the server-side state management mechanism. 1) Session creation and generation of unique IDs, 2) IDs are passed through cookies, 3) Server stores and accesses session data through IDs, 4) User authentication and status management are realized, improving application security and user experience.

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

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),

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft