


Detailed explanation of the underlying operating mechanism of PHP
In this article, we mainly share with you a detailed explanation of the underlying operating mechanism of PHP. First, we will share with you the design concepts and characteristics of PHP, the four-layer system of PHP, etc. We hope it can help you.
1. The design concept and characteristics of PHP
Multi-process model: Since PHP is a multi-process model, different requests do not interfere with each other, which ensures that one request will hang The loss will not affect the overall service. Of course, with the development of the times, PHP has already supported the multi-threading model.
Weakly typed language: Unlike C/C++, Java, C# and other languages, PHP is a weakly typed language. The type of a variable is not determined at the beginning. It is determined during operation and implicit or explicit type conversion may occur. The flexibility of this mechanism is very convenient and efficient in web development. The details will be discussed in PHP later. Variables are detailed in.
Interpreted language: PHP is different from C/C++, Java, C# and other compiled languages in the running steps. PHP needs to be parsed into a compiled language through lexical and syntactic analysis first. , to run! Therefore, PHP is not suitable for large-scale applications such as high performance or big data calculations. Although there is no difference between 0.001 seconds and 0.1 seconds for browser users, it is not suitable for other fields
The engine (Zend) + component (ext) mode reduces internal coupling.
The middle layer (sapi) isolates the web server and PHP.
The syntax is simple and flexible, without too many specifications. Shortcomings lead to mixed styles, but no matter how bad a programmer is, he will not write a program that is too outrageous and endangers the overall situation.
2. PHP’s four-layer system
The core architecture of PHP is as shown below:
From the picture It can be seen that PHP is a 4-layer system from bottom to top:
Zend engine: Zend is implemented entirely in pure C and is the core part of PHP. It translates PHP code (lexical , grammar parsing and a series of compilation processes) to process executable opcodes and implement corresponding processing methods, implement basic data structures (such as hashtable, oo), memory allocation and management, and provide corresponding api methods for external calls. It is 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 built-in functions (such as array series), standard libraries, etc. are all passed through extension, 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 enables PHP to interact with peripheral data through a series of hook functions. This is very elegant for PHP. With a successful design, PHP itself has been successfully decoupled and isolated 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.
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 applications through webserver, and using Run in script mode, etc.
If PHP is a car, then the framework of the car is PHP itself, Zend is the engine (engine) of the car, and the various components under Ext are the wheels of the car. Sapi can see It is a road, and cars can run on different types of roads, and the execution of a PHP program means that the car runs on the road. Therefore, we need: a high-performance engine + the right wheels + the right track.
3. Sapi
As mentioned above, Sapi allows external applications to exchange data with PHP through a series of interfaces and implement specific processing methods according to different application characteristics. We commonly see Some of the sapis are:
apache2handler: This is the processing method when using apache as the webserver and running in mod_PHP mode. It is also the most widely used one now.
cgi: This is another direct interaction method between webserver and PHP, which is the famous fastcgi protocol. In recent years, fastcgi+PHP has been used more and more, and it is also asynchronous. The only method supported by webserver.
cli: Application mode for command line calls
4. PHP execution process&opcode
Let’s take a look first The process through which PHP code is executed.
As you can see from the picture, PHP implements a typical dynamic language execution process: after getting a piece of code, after lexical analysis, syntax analysis and other stages, the source program will be translated into instructions (opcodes). The ZEND virtual machine then executes these instructions in sequence to complete the operation. PHP itself is implemented in C, so the functions ultimately called are all C functions. In fact, we can regard PHP as a software developed in C.
The core of PHP execution is the translated instructions, that is, opcode.
Opcode is the most basic unit of PHP program execution. An opcode consists of two parameters (op1, op2), return value and processing function. The PHP program is ultimately translated into the sequential execution of a set of opcode processing functions.
Several common processing functions:
PHP
##123456 | ##ZEND_ASSIGN_SPEC_CV_CV_HANDLER : Variable allocation ($a=$b) ZEND_DO_FCALL_BY_NAME_SPEC_HANDLER: Function call ZEND_CONCAT_SPEC_CV_CV_HANDLER: String concatenation $a.$b ZEND_ADD_SPEC_CV_CONST_HANDLER: Addition $a+2 ZEND_IS_EQUAL_SPEC_CV_CONST: Judge equality $a== 1 ZEND_IS_IDENTICAL_SPEC_CV_CONST: Judgment equal $a===1 |
1 2 3 4 5 6 7 8 9 10 |
##getKeyHashValueh;index=n&nTableMask ;Bucket*p=arBucket[index];while(p){if((p->h==h)&(p-> ;nKeyLength==nKeyLength)){RETURNp->data; }p=p->next;}RETURNFALTURE; |
2 3 4 5 | IS_LONG -> lvalue
IS_DOUBLE -> dvalue IS_ARRAY -> ht IS_STRING -> str IS_RESOURCE -> lvalue |
1 2 3 4 5 6 7 8 |
##$res=$strA.$strB and $res=“$strA$strB” In this case, zend will malloc a piece of memory again and process it accordingly. The speed is generally $strA=$strA.$strBThis is the fastest. zend will directly relloc based on the current strA to avoid repeated copying $res=$intA.$intB This is slower because it requires implicit format conversion and the actual writing In the program, you should also pay attention to avoid $strA=sprintf(“%s%s”,$strA.$strB);This will be the slowest way, because sprintf PHP is not a language structure. It takes a lot of time to identify and process the format. In addition, the mechanism itself is malloc. However, the sprintf method is the most readable, and in practice it can be chosen flexibly according to specific circumstances. |
The above is the detailed content of Detailed explanation of the underlying operating mechanism of PHP. For more information, please follow other related articles on the PHP Chinese website!

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python are both high-level programming languages that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.


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

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

Hot Article

Hot Tools

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

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.

WebStorm Mac version
Useful JavaScript development tools

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

Zend Studio 13.0.1
Powerful PHP integrated development environment