Study Notes on the Mechanisms and Principles of PHP
Preface
Often write cgi in php, but realized I don’t understand how it works, so learning about it.
What is PHP
PHP is a dynamic language for web development, a software framework implemented in C and containing a large number of components.
- Multi-process model: PHP is a multi-process model. Different requests do not interfere with each other, i.e., one request hangs without affecting the whole service (using process control functions to create child processes, execute programs, handle signals, etc.). (PHP can also support a multi-threaded model.
A process has a complete virtual address space that exists independently of a thread; a thread is part of a process, has no address space of its own, and shares all resources allocated to the process with other threads within the process.
Difference: (1) Processes have a separate space address, after a process crashes, it will not affect other processes in the protection mode. (2) Threads are just different execution paths of a process. Threads have their own stacks and local variables, but there is no separate address space between threads, and the death of a thread is equal to the death of the whole process.
- Weakly typed language: Unlike C, C++, C#, Java, etc., PHP is a weakly typed language. Unlike C++, C#, Java, etc., PHP is a weakly typed language, i.e., the type of a variable is not determined at the outset, but is determined at runtime and may be subject to implicit or explicit type conversion.
- Engine (Zend) + component (ext) model to reduce internal coupling
- middle layer (sapi) isolate web server and PHP
- simple and flexible syntax , not much specification
PHP’s core architecture
From the bottom up is as follows:
- Zend engine: Zend overall implementation in C, is the kernel part of PHP. It will translate the PHP code, the realization of the basic data structures, memory allocation mechanism and management, provides the appropriate api for external calls, is the core of everything.
- Extensions: around the Zend engine, extensions through the component-based way to provide a variety of basic services, we commonly see a variety of built-in functions, standard libraries, etc. are realized through the extension, the user can also be based on the need to achieve their own extension.
- Sapi: Server Application Programming Interface, that is, server-side application programming interface, is the middle layer of PHP and web server. sapi through the hook function, so that PHP can interact with external data, which will also decouple PHP and the upper tier of the application .
- Upper application: that is, we usually write the PHP program, through different sapi way to get a variety of application modes
Sapi
The common sapi we have are:
- apache2handler: apache as a webserver, using mod_PHP mode of operation when the processing, is now the most widely used one. 2. fast-cgi: this is another way of direct interaction between the webserver and PHP, that is, the famous fastcgi protocol, in recent times fastcgi + PHP has been increasingly used.
- fast-cgi: this is another way for webserver and PHP to interact directly, that is, the famous fastcgi protocol, recently fastcgi+PHP is getting more and more applications, but also asynchronous webserver is the only way to support. nginx is to parse the php through php-fpm (fast-cgi). nginx uses php-fpm (fast-cgi) to parse php.
- cli: Command Line Invocation Application Mode
php+apache vs php+nginx
apache parses php via mod_php; nginx parses php via php-fpm (fast-cgi).
- Whether the PHP interpreter is embedded within the web server process for execution
mod_php by embedding the PHP interpreter into the Apache process, can only be used with Apache. The disadvantage of this approach is a large memory footprint , such as processing CSS, JS and other static files is completely unnecessary to load the interpreter; and cgi and fast-cgi in the form of an independent process , as long as the corresponding Web server to achieve cgi or fast-cgi protocol, will be able to deal with PHP requests.
- The number of requests handled by a single process The mod_php and fast_cgi modes are able to handle multiple requests during the lifetime of each process, while the cgi mode destroys the process immediately after processing a request, which makes the performance of cgi very bad in highly concurrent scenarios. And fast-cgi also has the disadvantage of starting multiple processes, which takes up memory.
cgi and fast-cgi are communication protocols V1.0 and V2.0, while php-cgi and php-fpm are programs that implement such protocols. So it is php-cgi or php-fpm that handles specific requests, and they follow the fast-cgi protocol.
To summarize, if you have extremely high performance requirements, you can separate static and dynamic requests, in which case Nginx+php-fpm is a better choice.
PHP execution flow
After getting a piece of code, after lexical parsing, syntactic parsing and other stages, the source program will be translated into one instruction (opcodes), and then ZEND virtual machine in turn to execute these instructions to complete the operation.PHP itself is implemented in C, and therefore ultimately call the function is also C. In fact, we can regard PHP as a C development of the software.
PHP variables
PHP is a weakly typed language that does not strictly distinguish between types of variables per se. PHP variables can be categorized into simple types (int, string, boolean), aggregate types (array, resource, object), and constants (const). All variables are the same structure zval at the bottom. The zval consists of three main parts:
- type: specifies the type of the variable
- refcount&is_ref: used to implement reference counting
- value: stores the actual data (core) of the variable. Because of the multiple types to be stored, zvalue is a union, and weak typing is thus implemented.
Comparison with node.js
Multiple and single processes
As mentioned above, PHP is a multi-process model. Therefore, if the PHP code gets corrupted, it won’t bring down the entire server. PHP code runs only in its own process scope and when a request shows an error, it only affects a specific request, whereas in Node.js environment, all requests are served in a single process and when a request results in an unknown error, the entire server is affected. Below is the model of PHP and Node.js for handling Http requests. php+apache model
The node.js model
Multi-threaded and single-threaded
Node.js he used the JavaScript engine, then it is destined to be single-threaded, using asynchronous methods to open multiple tasks, without having to wait for the last task thread like php to use the end of the next use; PHP is also single-threaded but it borrows the Apache server to provide multi-threaded services. PHP is also single-threaded but it borrows the Apache server to provide multi-threaded services. Run mechanism comparison
Differences in handling high concurrency and large data volumes
php: optimize sql, use components, use caching, in order for the thread to end as soon as possible for the next task (up and down sequential execution); node: single-threaded, asynchronous, event-driven. Because it runs fast and doesn’t wait, if the results returned from the front are used later, you need to encapsulate the back and execute it as a callback function.




