I remember when I first started learning PHP, many interviewers would often ask me what PHP was, and the standard answer at that time was that PHP was a weakly-typed dynamic scripting language, open source, free, short for hypertext preprocessor.

This is just a superficial explanation, PHP is a tool for me, a hammer in my hand, although the hammer is often mocked as having nail raisers on both sides.

The characteristics of the PHP

Multiprocess model

PHP is a multi-process model design, non-interference in each other between requests, are the benefits of such a request failure will not affect other processes, as the beginning only for personal website design of a set of tools that there is nothing wrong, as the PHP application, obviously increased traffic this way is not suitable, Because starting a process of overhead on the request is not cost-effective, so now the basic PHP is running under the management of PHP – FPM, this is a PHP process manager, resident memory it started on some PHP process, when the request into the allocated when a process for processing, PHP recycling process when the process finishes, but does not destroy the process, This allows PHP to handle high-traffic access requests as well.

There are also multithreaded PHP solutions and coroutine based solutions that allow PHP to handle WEB requests more efficiently.

Weak type

Unlike JAVA or C/C++, PHP is a typed language. Variables do not need to be typed at the moment of declaration, and they can be typed explicitly or implicitly at runtime, which is one of the reasons why PHP is so fast and easy to develop applications.

other

The Zend engine + Ext extension pattern reduces internal coupling, making it easy to add and remove functionality from PHP itself.

The syntax is simple, there are not too many mandatory specifications, and the programming style can be developed in a procedural, object-oriented and functional way.

The architecture of the PHP

The architecture of the current mainstream VERSIONS of PHP, PHP7 and PHP5, is shown in the figure above, consisting of four layers: Zend engine, Extensions, SAPI interface, and upper layer applications from bottom to top.

The Zend engine

Zend engine is added to PHP after PHP4, is a rewrite of the original PHP interpreter, the overall use of C language for development, that is to say, PHP can be understood as a programming language written in C software, the engine is to translate PHP code into an intermediate language called Opcode. It is similar to JAVA’s ByteCode.

The engine performs four steps on the PHP code:

  1. Lexical analysis Scanning (Lexing) converts PHP code into language fragments.
  2. Parsing Parsing the Tokens into simple and meaningful expressions.
  3. Compilation Compilation, which compiles expressions into Opcode.
  4. Opcode is executed one at a time to implement the functionality expressed by the PHP code.

Extensions like APC and Opchche cache Opcode to speed up your PHP application, so you can skip the first three steps when the request comes in again.

The engine also implements basic data structures, memory allocation and management, and provides corresponding API methods for external calls.

Extensions extension

Common built-in functions and standard libraries are implemented in Extension, which is called the core PHP extension. Users can also install PHP extensions on their own.

SAPI

SAPI is the proxy between PHP and the external environment. It uses a series of hook functions to enable PHP to exchange data with the external environment. After abstracting the external environment, it provides a fixed and unified interface for the internal PHP, so that THE IMPLEMENTATION of PHP can not be affected by the complex external environment and maintain a certain degree of independence.

With SAPI decoupling, PHP can no longer worry about compatibility for different applications, and the application itself can be handled differently for its own characteristics.

The upper application

PHP programs written by programmers, whether they are Web applications or Cli applications, are upper-level applications, and the main job of PHP programmers is to write them.

conclusion

These things are obviously easier to understand if you’ve studied Android development, because the Android architecture looks very similar to the PHP architecture. When you understand you will understand why PHP after the founder would say PHP development is actually don’t need to frame, because PHP framework of design itself is a similar thing, if use metaphor car PHP itself is that the skeleton, the Zend engine, Ext is a wheel, the steering wheel, and other car components, SAPI is road.

For these contents, some people may feel a little useless, because many people are paranoid that they are just a driver, why should a driver know how a car runs, for understanding how to build a car is even more sniffy. I thought so, too, until I translated the Laravel document, and the author of Laravel gave me some inspiration and these words for you reading this article:

When working with any tool in the “real world,” you’ll be much more comfortable using it if you understand how it works. The same goes for application development. When you understand how your development tools work, you’ll be 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. With a thorough understanding of the framework, nothing will be “magical” and you will feel more confident building your application.