Understanding PHP principles opcodes (opcode) _php tutorial

Source: Internet
Author: User
Tags add numbers apc
Opcondes is a PHP script-compiled intermediate language, like the Java Code of Byte, or. NET MSL. (have never known ~)

As an example of this,
Copy CodeThe code is as follows:
echo "Hello World";
$a = 1 + 1;
echo $a;
?>

PHP executes this code in the following 4 steps (to be exact, it should be 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), converting tokens to simple and meaningful expressions
3.Compilation (compile), compile the expression into Opocdes
4.Execution (performs the compiled results), executes opcodes sequentially, one at a time, thus realizing the function of PHP script.

He also said: "Now some caches, such as APC, can make PHP cache opcodes, so that every 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 could be one of the reasons PHP is performing faster.

Then what is lexing? The students who have learned the principles of compiling should have some knowledge of the lexical analysis steps in the compiling principle, Lex is a basis table for lexical analysis. ZEND/ZEND_LANGUAGE_SCANNER.C will perform lexical analysis based on the PHP code entered by the ZEND/ZEND_LANGUAGE_SCANNER.L (lex file) to get a "word", PHP4.2 began to provide a function called Token_get_all, this function can be said 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] = =
)
[+] = Array
(
[0] = 305
[1] = 1
)
[+] =;
[+] = Array
(
[0] = 370
[1] = =
)
[+] = Array
(
[0] = 316
[1] = Echo
)
[+] = Array
(
[0] = 370
[1] = =
)
[+] =;
)

Analysis of the return result we can find that the source of strings, characters, spaces, will be returned as is. Each character in the source code appears in the appropriate order. And, other such as tags, operators, statements, will be converted to a two-part Array:token ID (that is, in the Zend internal change Token of the corresponding code, such as, t_echo,t_string), and the source of the original content.
Next, is the parsing stage, parsing first discards more spaces in the tokens array, and then converts the remaining tokens to a simple expression of one
Copy CodeThe code is as follows:
1.echo a constant string
2.add numbers together
3.store the result of the prior expression to a variable
4.echo a variable

Then change the compilation stage, it will tokens compiled into a op_array, each op_arrayd contains the following 5 parts:
Copy CodeThe code is as follows:
A 1.Opcode digital identification that indicates the type of operation for each op_array, such as Add, echo
2. results stored opcode results
3. Operand number 1 to opcode
4. Operand 2
5. Extended value 1 shaping to differentiate the overloaded operator

For example, our PHP code will be parsing into:
Copy CodeThe code is as follows:
* zend_echo ' Hello world '
* Zend_add ~0 1 1
* Zend_assign!0 ~0
* Zend_echo!0

Oh, you might ask, where is our $ A?

Well, this is about the operand, and each operand 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 that, depending on the op_type, saves the operand's value (CONST) or lvalue, respectively, with a different type ( var) and for Var, each var is not the same

Is_tmp_var, as the name implies, this is a temporary variable, save some Op_array results, so that the next op_array to use, this operand of u holds a pointer to the variable table of a handle (integer), this operand is generally used to start, such as ~0, A temporary variable that represents the unknown number No. 0 of the variable table

Is_var This is our general sense of the variable, they start with a $ expression

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

So it seems that our $ A is optimized to 0.
Summarize:


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

Add:
Apc,zend optimize is the reason to accelerate PHP
is because he can cache opcode.
is not a feature of the Zend engine itself
Zend Engine By default it's all going to be a reincarnation.

http://www.bkjia.com/PHPjc/322487.html www.bkjia.com true http://www.bkjia.com/PHPjc/322487.html techarticle Opcondes is a PHP script-compiled intermediate language, like the Java Code of Byte, or. NET MSL. (never know ~) For example, copy the code as follows: PHP echo "Hel ...

  • 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.