PHP内核-用户请求的开始和结束
安装过apache的人都知道,我们安装完PHP后,只是对apache进行配置,主要是添加mod_php5.so这个扩展,然后把apache重新启动,就可以正常使用PHP,这过程中我们从来没有手动启动过PHP的相关进程,那它是如何启动的呢?
它是随着apache的启动而启动的,安装在服务器上的PHP程序通过mod_php5.so模块和apache进行通信,其实在我前一篇博客里,我们知道,这个模块本质上是SAPI。在这篇博文我将讨论一次用户请求的过程,主要是如何发生通信的。
PHP主要包括三个模块,分别是内核部分,Zend引擎以及扩展部分。
内核主要用来处理请求、文件流、错误处理等相关操作;
Zend引擎(ZE)用以将源文件转换成机器语言,然后在虚拟机上运行它。
扩展层是一组函数、类库和流。PHP使用扩展层来执行一些特定的操作。
当一个请求到达之后,PHP的启动分为两个阶段:
- 第一个阶段是随apache的启动而启动的,也就是启动PHP模块,这个简单的过程在博文"Apache的PHP模块启动"有讨论。这个阶段生成一些环境变量,这些环境变量在SAPI的整个生命周期内都是可见的。
- 第二个阶段是一个请求到来之后,生成一些和请求相关的环境和变量。
在第二阶段,当一个页面请求到来时,SAPI层(APACHE)将控制权交给PHP层,此时PHP设置用于处理本次请求的环境变量。随后PHP调用各个模块的RINT方法,对请求进行初始化。前提是这些模块都在php.ini文件中有配置。如mysql模块的请求初始化:
PHP_RINIT_FUNCTION(mysql) { #if defined(ZTS) && MYSQL_VERSION_ID >= 40000 if (mysql_thread_init()) { return FAILURE; } #endif MySG(default_link)=-1; MySG(num_links) = MySG(num_persistent); /* Reset connect error/errno on every request */ MySG(connect_error) = NULL; MySG(connect_errno) =0; MySG(result_allocated) = 0; return SUCCESS; }再如session模块的请求初始化
PHP_RINIT_FUNCTION(session) { php_rinit_session_globals(TSRMLS_C); if (PS(mod) == NULL) { char *value; value = zend_ini_string("session.save_handler", sizeof("session.save_handler"), 0); if (value) { PS(mod) = _php_find_ps_module(value TSRMLS_CC); } if (!PS(mod)) { /* current status is unusable */ PS(session_status) = php_session_disabled; return SUCCESS; } } if (PS(serializer) == NULL) { char *value; value = zend_ini_string("session.serialize_handler", sizeof("session.serialize_handler"), 0); if (value) { PS(serializer) = _php_find_ps_serializer(value TSRMLS_CC); } } if (PS(mod) == NULL || PS(serializer) == NULL) { /* current status is unusable */ PS(session_status) = php_session_disabled; return SUCCESS; } if (PS(auto_start)) { php_session_start(TSRMLS_C); } return SUCCESS; }这个初始化会初始化$_SESSION变量。
在PHP关闭的时候,也分两个阶段,
- 第一阶段: 在一个页面结束的时候,会按照顺序调用各个模块的PHP_RSHUTDOWN_FUNCTION方法,清除所产生的变量和符号。如
PHP_RSHUTDOWN_FUNCTION(session) { php_session_flush(TSRMLS_C); php_rshutdown_session_globals(TSRMLS_C); return SUCCESS; }
- 第二阶段:最后,所有的请求都已处理完毕,SAPI也准备关闭了,PHP开始执行第二步:PHP调用每个扩展的MSHUTDOWN方法,这是各个模块最后一次释放内存的机会,如:
PHP_MSHUTDOWN_FUNCTION(session) { UNREGISTER_INI_ENTRIES(); #ifdef HAVE_LIBMM PHP_MSHUTDOWN(ps_mm) (SHUTDOWN_FUNC_ARGS_PASSTHRU); #endif ps_serializers[PREDEFINED_SERIALIZERS].name = NULL; memset(&ps_modules[PREDEFINED_MODULES], 0, (MAX_MODULES-PREDEFINED_MODULES)*sizeof(ps_module *)); return SUCCESS; }

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

SublimeText3 English version
Recommended: Win version, supports code prompts!

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 Mac version
God-level code editing software (SublimeText3)

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Atom editor mac version download
The most popular open source editor