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 structure and principle of PHP?

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article mainly explains "what is the structure and principle of PHP". Interested friends may wish to take a look at it. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn "what is the structure and principle of PHP"?

Characteristics of PHP

Multi-process model

PHP is designed with a multi-process model. The advantage is that requests do not interfere with each other, and failure of one request will not affect other processes. There is nothing wrong with such a design as a tool set that is initially used only for personal websites. As the application of PHP grows, it is obviously not appropriate to increase the number of visits, because the cost of starting a process is not cost-effective for massive requests. So now PHP is basically run under the management of PHP-FPM, this is a PHP process manager, it resides in memory to start some PHP processes to stand by, when the request comes in, it allocates a process for processing, and the PHP process reclaims the process after processing, but does not destroy the process, which enables PHP to cope with high-traffic access requests.

Of course, there are PHP multithreading solutions and co-program-based solutions, such as swoole, which allows PHP to process WEB requests more efficiently.

Weak type

Different from JAVA and PHP, PHP is a typed language. There is no need to determine its type at the moment of declaration, and the type will change explicitly or implicitly at run time, which is one of the reasons why PHP development and application is rapid and convenient.

Other

The mode of Zend engine + Ext extension reduces the internal coupling and makes it easy to add and remove functions for PHP itself.

The syntax is simple, there are not too many mandatory specifications, and the programming style can be developed either programmatically or in an object-oriented way, as well as functional.

Architecture of PHP

According to the current mainstream version of PHP, PHP7 and PHP5, the architecture is as shown in the above figure, which is mainly composed of four layers: Zend engine, Extensions extension, SAPI interface and upper application from bottom to top.

Zend engine

Zend engine is added to PHP after PHP4, is a rewrite of the original PHP interpreter, the overall development in C language, that is to say, PHP can be understood as a programming language software written in C, the role of the engine is to translate PHP code into an intermediate language called opcode, which is similar to JAVA's ByteCode (bytecode).

The engine performs four steps on the PHP code:

Lexical analysis of Scanning (Lexing) to convert PHP code into language fragments (Tokens).

Parse Parsing to convert Tokens into a simple and meaningful expression.

Compile Compilation, and compile the expression into Opcode.

Execute Execution, and execute Opcode sequentially, one at a time, to achieve the function expressed by the PHP code.

Extensions such as APC and Opchche can cache Opcode to speed up PHP applications, and they can be used to omit the first three steps when a request comes again.

The engine also implements the basic data structure, memory allocation and management, and provides the corresponding API methods for external calls.

Extensions extension

Common built-in functions and standard libraries are implemented through extension. These are called PHP core extensions, and users can also install PHP extensions according to their own requirements.

SAPI

SAPI is the abbreviation of Server Application Programming Interface, which is the server application programming interface in Chinese. It enables PHP to exchange data with the periphery through a series of hook functions. SAPI is the agent of PHP and external environment. After abstracting the external environment, it provides a set of fixed and unified interface for internal PHP, so that the implementation of PHP itself can not be affected by the complex external environment and maintain a certain degree of independence.

Through the decoupling of SAPI, PHP can no longer consider how to be compatible with different applications, and the application itself can achieve different processing methods according to its own characteristics.

Upper application

The PHP programs written by programmers, whether they are Web applications or applications running in Cli mode, are upper-level applications, and the main job of PHP programmers is to write them.

Summary

When you understand this, you will understand why the founder of PHP will say that PHP development actually does not need a framework, because the design of PHP itself is something similar to a frame, if you use a car analogy, PHP itself is the skeleton of the car, Zend is the engine, Ext is the wheel, steering wheel and other car components, SAPI is the road.

At this point, I believe you have a deeper understanding of "what is the structure and principle of PHP". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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

Internet Technology

Wechat

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

12
Report