


In-depth understanding of PHP kernel (2) Overview - PHP life cycle and Zend engine, -phpzend_PHP tutorial
In-depth understanding of the PHP kernel (2) Overview - PHP life cycle and Zend engine, -phpzend
This article refers to "In-depth understanding of the PHP kernel", address: https://github .com/reeze/tipi
1. SAPI interface
SAPI (Server Application Programming Interface) refers to the programming interface for specific PHP applications. PHP scripts can be executed in many ways: through a web server, the command line, or embedded in other programs.
Although executing scripts through a web server and a command line program looks very different, the actual process is the same. It's just that the web server method returns the php script execution results to the web server, and the command line prints the results to the console.
Script execution starts with the SAPI interface implementation, but different SAPI interface implementations will complete their specific work. For example, Apache’s mod_php SAPI implementation needs to initialize some information obtained from Apache. In the output content, the content Returned to Apache, other SAPI implementations are similar
2. Beginning and ending
(1) PHP will go through two stages after it starts executing: the starting stage before processing the request and the ending stage after the request. There are two processes in the beginning phase: the first process is the module initialization phase (MINIT). During the entire SAPI life cycle (such as the entire life cycle after Apache starts or the entire execution process of the command line program), this process only Do it once . The second process is the module activation phase (RINIT), which occurs in the request phase . For example, if a page is requested through a URL, module activation (RINIT request starts) will be performed before each request. For example, if PHP registers some extension modules, it will call back the MINIT function of all modules in the MINIT stage. The module performs some initialization work at this stage, such as registering constants, defining classes used by the module, etc. The module can implement these callback functions through the following macros during implementation:
<span>PHP_MINIT_FUNCTION(myphpextension) { </span><span>//</span><span> 注册常量或者类等初始化操作</span> <span>return</span><span> SUCCESS; }</span>
The basic environment of the PHP initialization script after the request arrives, such as creating an execution environment, including a symbol table that saves the variable names and value contents during PHP running, as well as a symbol table of all current functions, classes and other information. Then PHP will call the RINIT function of all modules. At this stage, each module can also perform some related operations. The module RINIR function is similar to the MINIT callback function:
<span>PHP_RINIT_FUNCTION(myphpextension) { </span><span>//</span><span> 例如记录请求开始时间 </span><span>//</span><span> 随后在请求结束的时候记录结束时间,这样我们就能记录下处理请求所花费的时间了</span> <span>return</span><span> SUCCESS; }</span>
After the request is processed, it enters the end phase. Generally, PHP will enter the end phase when the script is executed to the end or by calling the die() or exit() function. The end phase also has two links: one is to deactivate the module after the request is completed (RSHUTDOWN, corresponding to RINIT), and the other is to close the module when the SAPI life cycle ends (the web server exits or the command line script is executed and exits) (MSHUTDOWN, corresponding to MINIT).
<span>PHP_RSHUTDOWN_FUNCTION(myphpextension) { </span><span>//</span><span> 例如记录请求结束时间,并把相应的信息写入到日志文件中</span> <span>return</span><span> SUCCESS; }</span>
(2) Single-process SAPI life cycle
CLI/CGI mode PHP belongs to the single-process SAPI mode. This type of request is closed after processing the request once. SAPI life cycle: start-request start-request close-end
(3) Multi-process SAPI life cycle
Usually PHP is compiled into a module of Apache. Apache usually adopts multi-process mode. After Apache is started, it will fork multiple child processes. Each process has an independent memory space. Each child process will go through the start and end stages, but each The initial phase of the process only occurs after the process forks out, and it may be possible to process multiple requests during the entire process life cycle. The shutdown phase is only performed after Apache is shut down or the process is terminated. In these two phases, the request will be repeated with each request - the request shutdown link
(4) The multi-thread mode is similar to a process in a multi-process. The difference is that the request start-request closure process will be repeated in parallel throughout the life cycle of the process.
3. Zend Engine
Zend engine is the core of PHP implementation and provides the infrastructure for language implementation. For example, the implementation of PHP syntax, script compilation and running environment, expansion mechanism and memory management, etc. PHP here refers to the official PHP implementation (there is also Facebook's Hiphop, which has now developed into HHVM. So far (this project) PHP does not have a standard language specification), while PHP provides request processing and other Web server interface (SAPI)

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

Atom editor mac version download
The most popular open source editor

SublimeText3 Linux new version
SublimeText3 Linux latest version

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

Zend Studio 13.0.1
Powerful PHP integrated development environment

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.