Opcodes (operation code) that understands php principles)

Source: Internet
Author: User
Tags apc

Opcondes is an intermediate language after php script compilation, just like the Byte Code of Java or. net msl. (I have never understood it ~)

For example
Copy codeThe Code is as follows:
<? Php
Echo "Hello World ";
$ A = 1 + 1;
Echo $;
?>

PHP executes the Code in the following four steps (it should be the PHP language engine Zend)
Copy codeThe Code is as follows:
1. Scanning (Lexing) (SCAN) to convert PHP code into language snippets (Tokens)
2. Parsing (syntax analysis) converts Tokens into simple and meaningful expressions.
3. Compile the expression into Opocdes
4. Execution (Execution of compiled results): Execute Opcodes one by one each time to implement the PHP script function.

He also mentioned: "The current Cache, such as APC, can make PHP Cache Opcodes. In this way, when a request comes, you do not need to repeat the previous three steps, this greatly improves the execution speed of PHP. "This may be one of the reasons why php Execution is faster ~

So what is Lexing? Those who have learned the compilation principles should have some knowledge about the lexical analysis steps in the compilation principles. Lex is a basis table for lexical analysis. Zend/zend_language_scanner.c will input PHP code based on Zend/zend_language_scanner.l (Lex file) for lexical analysis to get a "word". PHP4.2 starts to provide a function called token_get_all, this function can introduce a piece of PHP code Scanning into Tokens;
If you use this function to process the PHP code we mentioned at the beginning, the following results will be obtained:
Copy codeThe Code is as follows:
Array
(
[0] => Array
(
[0] = & gt; 367
[1] => Array
(
[0] = & gt; 316
[1] => echo
)
[2] => Array
(
[0] = & gt; 370
[1] =>
)
[3] => Array
(
[0] = & gt; 315
[1] => "Hello World"
)
[4] =>;
[5] => Array
(
[0] = & gt; 370
[1] =>
)
[6] => =
[7] => Array
(
[0] = & gt; 370
[1] =>
)
[8] => Array
(
[0] = & gt; 305
[1] => 1
)
[9] => Array
(
[0] = & gt; 370
[1] =>
)
[10] => +
[11] => Array
(
[0] = & gt; 370
[1] =>
)
[12] => Array
(
[0] = & gt; 305
[1] => 1
)
[13] =>;
[14] => Array
(
[0] = & gt; 370
[1] =>
)
[15] => Array
(
[0] = & gt; 316
[1] => echo
)
[16] => Array
(
[0] = & gt; 370
[1] =>
)
[17] =>;
)

After analyzing the returned results, we can find that all strings, characters, and spaces in the source code are returned as they are. The characters in each source code appear in the corresponding sequence. Other statements, such as tags, operators, and statements, are converted into an Array containing two parts: Token ID (that is, the corresponding code for changing Token within Zend, for example, t_ECHO, T_STRING), and the original content in the source code.
Next, it is the Parsing phase. Parsing first discards more than spaces in the Tokens Array, and then converts the remaining Tokens into a simple expression.
Copy codeThe Code is as follows:
1. echo a constant string
2. add two numbers together
3. store the result of the prior expression to a variable
4. echo a variable

Then the Compilation phase is changed. It will compile Tokens into op_array, and each op_arrayd contains the following five parts:
Copy codeThe Code is as follows:
1. indicates the operation type of each op_array, such as add and echo.
2. Store the Opcode results.
3. operands 1 to Opcode
4. operand 2
5. The extended value is an integer to distinguish the overloaded operator.

For example, our PHP code will be Parsing:
Copy codeThe Code is as follows:
* ZEND_ECHO 'Hello world'
* ZEND_ADD ~ 0 1 1
* ZEND_ASSIGN! 0 ~ 0
* ZEND_ECHO! 0

You may ask, where is our $?

Well, we will introduce the operands. each operand consists of the following two parts:

A) op_type: IS_CONST, IS_TMP_VAR, IS_VAR, IS_UNUSED, or IS_CVb) u. a consortium stores the value of this operand (const) with different types according to op_type) or the left value (var). For var, each var is different.

IS_TMP_VAR, as the name suggests, is a temporary variable that stores the results of some op_array for the next op_array. The u of this operand stores a handle (integer) pointing to the variable table ), this type of operand is generally used ~ For example ~ 0 indicates the temporary variable of the variable table No. 0 unknown

IS_VAR is a variable in the general sense. They start with $.

IS_CV indicates a cache mechanism used by the compiler after ZE2.1/PHP5.1. This variable stores the address of the variable referenced by it. When a variable is referenced for the first time, the variable will be used up by the CV, and you do not need to search for the active symbol table again for future reference of this variable. The CV variable starts! .

In this case, our $ a is optimized! 0.
Summary:


1. the php Execution sequence is:-> php Program
-> Scanning (scan to Tokens (Language Segment ))
-> Parsing (syntax analysis converts Tokens into simple and meaningful expressions)
-> Compilation (compile, compile the expression into Opocdes (Operation Code ))
-> Execution (sequential Execution of compiled results)
2. the Cache mechanism of APC (Alternative PHP Cache) can Cache Opcodes, so that the first three steps do not need to be repeated when the next request comes, this greatly improves the execution speed of PHP. This may be one of the reasons why php runs faster.

Supplement:
Apc and zend optimize accelerate php
It is because it can cache opcode
It is not a built-in function of the zend engine.
By default, the zend engine is required for any item in the cycle.

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: info-contact@alibabacloud.com 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.