Understanding the Principles of PHP opcodes (opcode)

Source: Internet
Author: User
Tags apc array contains execution expression php code php script zend
Opcondes is a PHP script-compiled intermediate language, like the Java byte Code, or. NET MSL. (have never known ~)

For example in the text
Copy CodeThe code is as follows:
<?php
echo "Hello World";
$a = 1 + 1;
echo $a;
?>

PHP executes this code through the following 4 steps (specifically, PHP's language engine Zend)
Copy CodeThe code is as follows:
1.Scanning (lexing) (scan), convert PHP code to language fragment (tokens)
2.Parsing (parsing), converts tokens into simple and meaningful expressions
3.Compilation (compiled) to compile the expression into an opocdes
4.Execution (perform the compiled results), execute opcodes in sequence, one at a time, to achieve the function of PHP script.

He also said: "Now some cache such as APC, you can make PHP cache live opcodes, so that every time there is a request, there is no need to repeat the previous 3 steps, so as to greatly improve the speed of PHP execution." "This may be one of the reasons PHP executes faster," he said.

Then what is lexing? Have learned the principle of compiling students should be the principle of the lexical analysis of the process to understand that Lex is a lexical analysis of the basis table. ZEND/ZEND_LANGUAGE_SCANNER.C will be based on the ZEND/ZEND_LANGUAGE_SCANNER.L (lex file) to enter the PHP code for lexical analysis, so as to get a "word", PHP4.2 began to provide a function called Token_get_all, this function can tell a section of PHP code scanning into tokens;
If we use this function to process the PHP code we mentioned at the beginning, we will get the following result:
Copy CodeThe code is as follows:
Array
(
[0] => Array
(
[0] => 367
[1] => Array
(
[0] => 316
[1] => Echo
)
[2] => Array
(
[0] => 370
[1] =>
)
[3] => Array
(
[0] => 315
[1] => "Hello World"
)
[4] =>;
[5] => Array
(
[0] => 370
[1] =>
)
[6] => =
[7] => Array
(
[0] => 370
[1] =>
)
[8] => Array
(
[0] => 305
[1] => 1
)
[9] => Array
(
[0] => 370
[1] =>
)
[Ten] => +
[One] => Array
(
[0] => 370
[1] =>
)
[of] => Array
(
[0] => 305
[1] => 1
)
[=>];
[=>] Array
(
[0] => 370
[1] =>
)
[A] => Array
(
[0] => 316
[1] => Echo
)
[[] => Array
(
[0] => 370
[1] =>
)
[=>];
)

Analyzing this return result, we can find that the strings, characters, and spaces in the source code will be returned as they are. The characters in each source code appear in the appropriate order. Others, such as tags, operators, and statements, are converted into a array:token ID that contains two parts (that is, the corresponding code for the Token within the Zend, such as t_echo,t_string), and the original content in the source code.
Next, is the parsing phase, parsing will first discard more than the space in the tokens array, and then convert 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 change the compilation stage, it will compile tokens into a op_array, each op_arrayd contains the following 5 parts:
Copy CodeThe code is as follows:
The identification of 1.Opcode digits, indicating the type of operation for each op_array, such as Add, echo
2. results deposited opcode results
3. Operation number 1 to opcode operand
4. Operation number 2
5. Extended value 1 cosmetic 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

Oh, you may ask, our $a went there?

Well, this is about the operands, each of which consists of the following two parts:

A) Op_type: for Is_const, Is_tmp_var, Is_var, is_unused, or IS_CVB) u, a consortium, depending on the op_type, preserves the operand's value (CONST) or the left value in different types ( var) and for Var, each var is not the same

Is_tmp_var, as the name suggests, this is a temporary variable, save some Op_array results, so that the next op_array use, this operand of the U-hold a pointer to a variable table (integer), this operand generally with a ~ start, such as ~0, An unknown temporary variable representing the No. 0 number of the variable table

Is_var This is a variable in our general sense, and they begin with $ to denote

IS_CV says ZE2.1/ PHP5.1 later compiler uses a cache mechanism, which holds the address of the variable referenced by it, when a variable is first referenced, it will be CV up, then the reference to this variable will not need to find the active symbol table again, CV variable to! The beginning represents.

So, our $a has been optimized for 0.
Summarize:


The order of execution of 1.php is:-> PHP program
-> by scanning (scan converted to tokens (language fragment))
-> is parsing (parsing, converting tokens into simple and meaningful expressions)
-> compilation (compile, compile expression into opocdes (opcode))
-> Execution (sequential execution of compiled results)
2. APC (alternative PHP cache) caching mechanism can cache live opcodes, so that the next time there is a request, there is no need to repeat the previous 3 steps, which can greatly improve the speed of PHP execution. This may be one of the reasons PHP executes faster

Add:
Apc,zend optimize is the reason to speed up PHP
Just because he can cache opcode.
is not a function of the Zend engine itself.
The Zend engine defaults to a cycle in which any one of them has to go.

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.