The operating mechanism and principle of PHP bottom

Source: Internet
Author: User
Tags php open source project sapi sprintf nginx server zend

PHP is a dynamic language for web development. Specifically, it is a software framework that implements a large number of component modules in the C language. is a powerful UI framework.

In short, PHP Dynamic language execution process: After getting a piece of code, after the lexical parsing, parsing and other stages, the source program will be translated into a command (opcodes), and then zend the virtual machine to execute the instructions in sequence to complete the operation. PHP itself is implemented in C, so the final call is also the C function, in fact, we can think of PHP as a C developed software.

I. Design concept and features of PHP

1, multi-process model: Because PHP is a multi-process model, different requests between non-interference, so as to ensure that a request to hang out will not affect the overall service, PHP is also early support for multithreaded models .

2, weak type language: and C/S, JAVA, C # and other languages, PHP is a weak type of language. The type of a variable is not determined from the beginning, and the implicit or displayed type conversions are determined and possible in the run, and the flexibility of this mechanism is very convenient and efficient in web development, and is detailed in the following PHP variables.

3, the engine (Zend) + component (EXT) mode reduces the internal coupling.

4, middle tier (SAPI) SAPI full name is the server application programming Interface isolated Web server and PHP.

5, the syntax is simple and flexible, not too many specifications. Disadvantages lead to a mix of styles.

Second, PHP four-tier system

PHP core architecture such as: (I myself according to the online map has PS, compare image; is my personal website; prepare to operate a PHP open source project; Before the I was the website long)

PHP is a 4-tier system from the bottom down:

1, Zend Engine: Zend Whole with pure C implementation, is the core part of PHP, he will php code translation (lexical, grammatical parsing, such as a series of compilation process) for the execution of opcode processing and implementation of the corresponding processing methods, the realization of the basic data structure (such as: Hashtable, OO), Memory allocation mechanism and management, provides the corresponding API method for external invocation, is the core of all, all the peripheral functions are around Zend implementation.

2, Extensions: Around the Zend Engine, Extensions through a component-based approach to provide a variety of basic services, our common various built-in functions (array series), standard library, etc. are through the extension to achieve, Users can also implement their own extension typical applications as needed.

3, SAPI:SAPI full name server application programming Interface, that is, the service-side application programming interface, SAPI through a series of hook functions, so that PHP can interact with the peripheral data, which is PHP very elegant and successful design, By SAPI successfully decoupling PHP itself from the upper-layer application, PHP can no longer consider how to implement compatibility for different applications, and the application itself can be handled differently for its own characteristics.

4, the upper application: This is the PHP program we usually write, through different Spai way to get a variety of application patterns, how to implement the Web application through webserver, the command line has been scripted operation and so on.

We need: Engine with excellent performance (Zend) + suitable wheels (EXT) + correct runway (SAPI).

Third, Sapi

SAPI through a series of interfaces, so that external applications can exchange data with PHP and can be implemented according to different application characteristics of specific processing methods, we have a number of common SAPI are:

1,Apache2handler: This is the Apache as a webserver, the use of mod_php mode operation of the process, is now the most widely used.

2, CGI: This is webserver and PHP direct another way of interaction, that is, the famous fastcgi protocol, in the recent fastcgi+php get more and more applications, but also the only way to support asynchronous webserver ; typical application nginx server; fastcgi White Point is an extension of PHP

    • fastcgi process Manager itself initializes, launches multiple CGI interpreter processes (visible multiple php-cgi) and waits from the Web The connection to the server.
    • When a client request arrives at the Web server, the FASTCGI process manager selects and connects to a CGI interpreter. WEB server sends CGI environment variables and standard input to the FASTCGI child process php-cgi.
    • fastcgi child process finishes processing, returns the standard output and error information from the same connection to the Web Server. When the fastcgi child process closes the connection, the request is processed to completion. The fastcgi child process then waits and processes the next connection from the FASTCGI process Manager (running in Web server). In CGI mode, php-cgi exits here.
    • in the above case , you can imagine how slow CGI is usually. Every Web request PHP must re-parse php.ini, reload all extensions, and initialize all data structures. With fastcgi, all of this occurs only once when the process is started. An additional benefit is that the persistent database connection (persistent connection) can work.

3. CLI: Application Mode for command line invocation

The command Line interface (English: command-line interface, abbreviation: CLI) is the most widely used user interface before the graphical user interface gets popular, it usually does not support the mouse, the user through the keyboard input instruction, the computer receives the instruction, executes. Others call it the character user interface (CUI).
It is generally assumed that the command line interface (CLI) does not have a graphical user interface (GUI) for user-friendly operation. Because the command line interface software usually requires user memory operation of the command, but due to its own characteristics, the command line interface to save the computer system resources than the graphical user interface. With the command-line interface, it is often faster to use a graphical user interface than to use a GUI when memorizing commands. As a result, the graphical user interface of the operating system retains an optional command-line interface.

Iv. implementation process of PHP &opcode

PHP Dynamic Language execution process: After getting a piece of code, after lexical parsing, parsing and other stages, the source program will be translated into a command (opcodes), and then zend the virtual machine to execute the instructions in sequence to complete the operation. PHP itself is implemented in C, so the final call is also the C function, in fact, we can think of PHP as a C developed software.

The core of PHP execution is a translation of an instruction, but also opcode.

OpCode is the most basic unit of PHP program execution.

In the field of computer science, the opcode (Operation code, OPCode) is used to describe the machine language instruction, specifying the part of the machine code to perform some operation, and the instruction format and specification of the OPCode are specified by the directive specification of the processor.

A opcode consists of two parameters (OP1,OP2), a return value, and a handler function. The PHP program is eventually translated into a set of opcode processing functions in the order of execution.

A few common processing functions:

Zend_assign_spec_cv_cv_handler: Variable assignment ($a=$b$a. $b Zend_add_spec_cv_const_handler $a+2$a==1$a===1    

V. hashtable--CORE Data structure

Hashtable is the core data structure of Zend, in PHP almost used to implement all common functions, we know that the PHP array is its typical application, in addition, within the Zend, such as function symbol table, global variables, etc. are also based on hash table has the following characteristics:

1, support the typical key->value query

2, can be used as an array

3. Adding and Removing Nodes is O (1) complexity

4. Key supports mixed type: Associative number combined index array exists

5. Value supports mixed type: Array ("string", 2332)

6, support linear traversal: such as foreach

Zend Hash table implements a typical hash list hash structure and provides a forward and backward traversal of the array by attaching a doubly linked list. Its structure is as follows:

You can see: In the hash table, both the Key->value form of the hash structure, as well as the bidirectional linked list mode, makes it very convenient to support fast lookup and linear traversal.

1. Hash structure: The hash structure of Zend is a typical hash table model, which solves the conflict by means of the linked list. It is important to note that the Zend hash table is a self-growing data structure that, when the number of hash tables is full, dynamically expands and re-positions the elements in twice-fold ways. The initial size is 8. In addition, in the Key->value Quick Find, Zend itself has done some optimization, through the way of space-changing time to speed up. For example, in each element, a variable nkeylength is used to identify the length of the key for quick determination.

2, two-way list: Zend hash table The linear traversal of elements is realized through a linked list structure. In theory, it is enough to do a traversal using a one-way list, the reason is to use a doubly linked list, the main purpose is to quickly delete, avoid traversal. Zend hash table is a composite structure that, when used as an array, supports common associative arrays that can also be used as sequential index numbers, and even allow a mixture of 2. PHP associative arrays: Associative arrays are typical hash_table applications. A single query process takes the following steps (as you can see from the code, this is a common hash query process and adds some quick judgments to speed up lookups.) ):

1 Getkeyhashvalue H;2Index = n &Ntablemask;3Bucket *p =Arbucket[index];4  while(p) {5        if((P->h = = h) && (p->nkeylength = =nkeylength)) {6               RETURNP->data; 7         }8P=p->Next;9 }Ten RETURNFalture;

4. PHP indexed array: The index array is our common array, accessed by subscript. For example $arr [0],zend Hashtable internal normalization, for the index type key is also assigned a hash value and Nkeylength (0). The internal member variable nnextfreeelement is the maximum ID currently assigned to it, and automatically adds one after each push. It is this normalization that enables PHP to implement associative and non-associative blending. Because of the specificity of the push operation, the index key in the PHP array order is not determined by the subscript size, but by the push of the successive decision. For example $arr [1] = 2; $arr [2] = 3; For a double type of key,zend Hashtable will treat him as an index key

Vi. PHP variables

PHP is a weakly typed language that itself does not strictly differentiate between types of variables. PHP does not need to specify a type when declaring a variable.

PHP may perform implicit conversions of variable types during a program run. As with other strongly typed languages, you can also display type conversions in your program.

PHP variables can be divided into simple types (int, string, bool), collection type (array resource object), and constant (const). All of the above variables are the same structure Zvalat the bottom.

Zval mainly consists of three parts:

    • Type: Specifies the types described by the variable (integers, strings, arrays, etc.)
    • Refcount&is_ref: Used to implement reference counting (described later)
    • Value: The core part that stores the actual data of the variable

Zvalue is the actual data used to hold a variable. Because there are many types to store, Zvalue is a union and thus a weak type is implemented.

The PHP variable type and its actual storage correspondence are as follows:

    • reference counting is widely used in areas such as memory reclamation, string manipulation, and so on . A variable in PHP is a typical application of reference counting. The reference count of Zval is implemented through member variables is_ref and Ref_count, and by reference counting, multiple variables can share the same data. Avoid the large amount of consumption caused by frequent copying. When an assignment is performed, Zend points the variable to the same zval while ref_count++, corresponding to the ref_count-1 when the unset operation. Only Ref_count minus 0 o'clock will actually perform the destroy operation. If it is a reference assignment, Zend modifies is_ref to 1.

    • PHP variables share data by reference counting, so what if you change one of the variable values ? When attempting to write a variable, Zend discovers that the variable points to the Zval is shared by more than one variable, it copies a copy of Ref_count 1 zval, and decrements the zval of the original refcount, this process is called "Zval separation." It can be seen that the copy operation is only performed when there is a write operation, so it is also called copy-on-write (copy-on-write) for reference variables, which are required and non-reference Zend, the variables that reference the assignment must be bundled, and modifying a variable modifies all the bundle variables. Integer, floating-point number is one of the basic types in PHP and is also a simple variable. for integers and floating-point numbers, the corresponding values are stored directly in the Zvalue. The types are long and double, respectively.

    • As can be seen from the zvalue structure, for the integer type, and C and other strongly typed languages, PHP is not distinguished from int, unsigned int, long, long long, and so on, for it, there is only one type of integer is long. Thus, it can be seen that in PHP, the value range of integers is determined by the number of compiler bits rather than fixed.

    • for floating-point numbers, like integers, it does not distinguish between a float and a double but only a double type . What if the integer range is out of bounds in PHP? This situation is automatically converted to double type, this must be careful, a lot of trick are generated from this.

    • like integers, character variables are the underlying type and simple variable in PHP . The Zvalue structure shows that in PHP, strings are made up of pointers and length structures that point to actual data, which is similar to string in C + +. Since the length is represented by an actual variable, and unlike C, its string can be 2 binary (inclusive), while in PHP, the string length strlen is an O (1) operation. PHP will reallocate memory to generate new strings when adding, modifying, appending string operations. Finally, for security reasons, PHP will still add the end of a string when it is generated.

Common string stitching method and speed comparison: Suppose there are 4 variables: $strA = ' 123 '; $strB = ' 456 '; $intA = 123; intb=456;

Now for the following several string stitching method to do a comparison and description:

1 $res=$strA.$strB and $res=$strA $STRB"2 in this case, Zend will re-malloc a piece of memory and handle it accordingly, at a speed generally3 $strA=$strA.$strB4 This is the fastest, Zend will be directly relloc on the current stra basis, to avoid duplicate copies5 $res=$intA.$intB6 This is slower, because implicit format conversion is required, and the actual program should be careful to avoid7 $strA=sprintf("%s%s",$strA.$strB);8This is the slowest way, because sprintf is not a language structure in PHP, it takes more time to recognize and process the format itself, and the mechanism itself

also malloc memory. However, the sprintf is the most readable, in practice can be flexibly selected according to the specific circumstances.

    • the PHP array is naturally implemented by Zend Hashtable . How is the foreach operation implemented? A foreach to an array is done by traversing a doubly linked list in the Hashtable. For indexed arrays, the foreach traversal efficiency is much higher than for a for, eliminating the Key->value lookup. The count operation calls the Hashtable->numofelements,o (1) operation directly. For a string such as ' 123 ', the Zend is converted to its integer form. $arr [' 123 '] and $arr[123] are equivalent

    • The resource type variable is one of the most complex variables in PHP and is a composite structure. PHP's Zval can represent a wide range of data types, but it is difficult to adequately describe the custom data types. Because there is no effective way to depict these composite structures, there is no way to use traditional operators for them. To solve this problem, you only need to refer to pointers through an inherently arbitrary identifier (label), which is called a resource.

In Zval, for Resource,lval to be used as a pointer, point directly to the address where the resource resides. Resource can be any composite structure, we are familiar with mysqli, Fsock, memcached , etc. are resources.

How to use resources:

  • Registration: For a custom data type, you want to use it as a resource. Registration is required first, and Zend assigns it a globally unique label.
  • Get a resource variable: for a resource, Zend maintains a hash_tale that id-> the actual data. For a resource, only its ID is recorded in Zval. Fetch is returned by the ID in hash_table to find the specific value.
  • Resource destruction: The data types of the resources are varied. Zend itself has no way of destroying it. Therefore, the user is required to provide the destruction function when registering the resource .
    When a resource is unset, Zend calls the corresponding function to complete the destructor. Remove it from the global Resource table at the same time.
  • A resource can reside for a long time, not just after all the variables referencing it go out of scope, even after a request has ended and a new request has been made. These resources are called persistent resources because they persist throughout the life cycle of the SAPI, unless specifically destroyed. In many cases, the persistence of resources can improve performance to some extent. For example, our common mysql_pconnect, persistent resources allocate memory through PEMALLOC, which is not released at the end of the request. For Zend, there is no distinction between the two.

  • How are local variables and global variables implemented in PHP ? For a request, PHP can see two symbol tables (symbol_table and active_symbol_table) at any time, where the former is used to maintain global variables. The latter is a pointer to the currently active variable symbol table, and when the program enters a function, Zend assigns it a symbol table x and points active_symbol_table to a. The distinction between global and local variables is realized in this way.
  • Gets the value of the variable: the PHP symbol table is implemented through hash_table, assigning a unique identifier to each variable, and retrieving the corresponding zval from the table when it gets returned.
  • Global variables are used in functions: In functions, we can use global variables by explicitly declaring global. A reference to a variable with the same name in Active_symbol_table is created in the symbol_table and is created if there is no variable with the same name in Symbol_table.

The operating mechanism and principle of PHP bottom

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.