I remember when I first started learning PHP, many interviewers would often ask me what PHP was. The standard answer at that time was that PHP is a weakly typed dynamic script programming language, open source. ,
free, is the abbreviation of hypertext preprocessor.
This is just a very superficial explanation. PHP is a tool for me, a hammer in my hand, although this hammer is often ridiculed as a hammer with nail removers on both sides.
Multi-process model
PHP is designed with a multi-process model. The advantage is that requests do not interfere with each other, and the failure of one request will not affect other processes. There is nothing wrong with this design as a tool set that was initially only used for personal websites. As PHP applications become larger and the number of visits increases, this method is obviously inappropriate because of the overhead of starting a process. It is not cost-effective to deal with massive requests, so now PHP is basically running under the management of PHP-FPM. This is a PHP process manager. It is resident in the memory and starts some PHP processes on standby. When a request comes in, a process is assigned to process it. After processing, the PHP process recycles the process but does not destroy the process. This allows PHP to cope with high-traffic access requests.
Of course, there are now PHP multi-threaded solutions and coroutine-based solutions that allow PHP to process WEB requests more efficiently.
Weak type
Unlike JAVA and C/C, PHP is a typed language. The type of a variable does not need to be determined at the moment it is declared. , and the type will also change explicitly or implicitly at runtime, which is one of the reasons why PHP development and application is fast and convenient.
Others
Zend engine Ext extension mode reduces internal coupling and can easily add and remove functions to PHP itself.
The syntax is simple and there are not many mandatory specifications. The programming style can be developed in either procedural or object-oriented way. Of course, functional style can also be used.
Taking the current mainstream versions of PHP, PHP7 and PHP5, the architecture is as shown in the figure above. It mainly consists of four layers of system. From bottom to top, they are Zend engine, Extensions extension, SAPI interface, and upper-layer application.
Zend Engine
Zend engine is PHP4 and will be added to PHP later. It is a rewrite of the original PHP interpreter and is developed entirely using C language, that is to say PHP can be understood as a programming language software written in C. The function of the engine is to translate the PHP code into an intermediate language called opcode, which is similar to JAVA's ByteCode (bytecode).
The engine will perform four steps on the PHP code:
Lexical analysis Scanning (Lexing), converting the PHP code into language fragments (Tokens). Parsing, converting Tokens into simple and meaningful expressions. Compilation Compilation, compiles expressions into Opcode. Execute Execution and execute Opcode sequentially, one at a time, to realize the functions expressed by the PHP code.
APC, Opchche These extensions can cache Opcode to speed up the running speed of PHP applications. Using them, you can omit the first three steps when the request comes again.
The engine also implements basic data structures, memory allocation and management, and provides corresponding API methods for external calls.
Extensions Extensions
Common built-in functions and standard libraries are implemented through extensions. These are called core extensions of PHP. Users can also install them according to their own requirements. PHP extension.
SAPI
SAPI is the abbreviation of Server Application Programming Interface. Chinese is the server application programming interface. It allows PHP to exchange data with peripherals through a series of hook functions. SAPI is the agent between PHP and the external environment. It abstracts the external environment and provides a fixed and unified interface for the internal PHP, so that PHP's own implementation 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 also implement different processing methods based on its own characteristics.
Upper-layer application
The PHP programs written by programmers, whether they are Web applications or applications running in Cli mode, are upper-layer applications. The main job of PHP programmers is to write them .
Summary
If you have studied android development, it will obviously be easier to understand these things, because the architecture of android looks very similar to the architecture of PHP. When you understand this, you will understand why the founder of PHP said that PHP development does not actually require a framework, because the design of PHP itself is something similar to a framework. If you use a car metaphor, PHP itself is the skeleton of the car. Zend is the engine, Ext is the wheels, steering wheel and other car components, and SAPI is the road.
Some people may find this content a bit useless, because many people are paranoid that they are just drivers. Why do drivers need to understand how a car runs? It is more important to understand how to build a car. It's scornful. I used to think so too, until when I was translating the Laravel documentation, the author of Laravel gave me some inspiration, and I also send these words to you who read this article:
When using any tool in the "real world", if you understand how the tool works, you will be more comfortable using the tool. The same goes for application development. When you understand how your development tools work, you'll become more comfortable using them.
The purpose of this document is to give you a better understanding of how the Laravel framework works and how it works. By having a comprehensive understanding of the framework, everything will be less "magical" and you will be more confident in building your applications.
The above is the detailed content of What is PHP - Overview of PHP's architecture and principles. For more information, please follow other related articles on the PHP Chinese website!