In-depth understanding of PHP opcode principles, in-depth understanding of opcode_php tutorials

Source: Internet
Author: User
Tags add numbers php database php regular expression

In-depth understanding of PHP opcode principles, in-depth understanding of opcode


This article describes the principle of opcode in PHP. Share to everyone for your reference, as follows:

OpCode is a PHP script-compiled intermediate language, like Java's bytecode, or. Net of MSL. This article is mainly based on "understanding OPcode" and the network, according to individual understanding and modification, special record down:

PHP Code:

<?php  echo "Hello World";  $a = 1 + 1;  echo $a;? >

PHP executes this code in the following 4 steps:

1. Scanning (lexing), convert the PHP code to a language fragment (Tokens)
2. Parsing, convert tokens to simple and meaningful expressions
3. Compilation, compile the expression into Opocdes
4. Execution, execute opcodes sequentially, one at a time, thus realizing the function of PHP script.

Note: 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.

First, ZEND/ZEND_LANGUAGE_SCANNER.C will parse the input PHP code according to 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;

<?php$tokens = Token_get_all (' <?php  echo ' Hello world ';  $a = 1 + 1;  echo $a;? > ');p rint_r ($tokens);? >

You will get the following results:

Array ([0] = = Array ([0] = = 367 [1] = <?php [2] = 1) [1] = = Array ([ 0] = = 370 [1] = [2] = 2) [2] = = Array ([0] = [1] = =/+/+/+/+/+/-echo [2] =& Gt 2) [3] = = Array ([0] = 370 [1] = [2] = 2) [4] = = Array ([0] = =  315 [1] = "Hello World" [2] = 2) [5] = =;      [6] = = Array ([0] = 370 [1] = [2] = 2) [7] = = Array ([0] = 309 [1] = = $a [2] = 3) [8] = = Array ([0] = 370 [1] = [2] = 3) [9] =&g T       = [ten] = = Array ([0] = 370 [1] = [2] = 3) [one] = = Array ([0] = 305 [1] = 1 [2] = 3) [] = = Array ([0] = 370 [1] = = [2] = 3) [1   3] = + [+] + = Array ([0] = = 370 [1] = =   [2] = 3) [[]] = Array ([0] = 305 [1] = 1 [2] + 3) [+] = =;      [+] = Array ([0] = 370 [1] = [2] = 3) [] = = Array ([0] = 316 [1] [+] = echo [2] = 4) [+]/= Array ([0] = = 370 [1] = [2] = 4) [2  0] = = Array ([0] = 309 [1] = = $a [2] = 4) [+] =;      [+] = Array ([0] = 370 [1] = [2] = 4) [+] = Array ([0] = 369 [1] =?> [2] = 5))

The returned result, the string in the source code, 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

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, to change the compilation stage, it will tokens compiled into a op_array, each op_arrayd contains the following 5 parts:

1. Identification of the opcode number, indicating 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, PHP code will be parsing into:

[Root@localhost html]#/usr/local/php/bin/php-dvld.active=1 hello.phpbranch analysis from Position:0return Foundfilename:    /var/www/html/hello.phpfunction name: (null) Number of ops:6compiled VARs:!0 = $aline   # op              Fetch     ext return operands-------------------------------------------------------------------------------  2   0 echo                           ' Hello+world '  3   1 ADD                       ~0   1, 1     2 ASSIGN                          ! 0, ~0  4 3   ECHO                           !0  6   4 RETURN                          1     5* Zend_handle_exceptionhello world2

Each operand is made up of the following two parts:

A) Op_type: for Is_const, Is_tmp_var, Is_var, is_unused, or IS_CV

b) U, a consortium that holds the value (const) or Lvalue (Var) of this operand in different types, depending on the op_type.

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, Represents the No. 0 unknown temporary variable of the variable table Is_var This is our general sense of the variable, they start with a $ IS_CV represents a cache mechanism used by the compiler after ze2.1/php5.1, which holds the address of the variable referenced by it, When a variable is referenced for the first time, it will be CV-the reference to this variable will not need to find the active symbol table again, CV variable to! The beginning indicates.

$a variables are optimized to! 0.

More interested in PHP related content readers can view the site: "PHP Math Skills Summary", "PHP operation Office Document Skills Summary (including word,excel,access,ppt)", "PHP array" operation Skills Daquan, " PHP Sorting algorithm Summary, "PHP Common traversal algorithm and skills summary", "PHP Data structure and algorithm tutorial", "PHP Programming Algorithm Summary", "PHP Regular Expression Usage summary", "PHP operation and operator Usage Summary", "PHP string Usage Summary" and " A summary of common PHP database operation techniques

I hope this article is helpful to you in PHP programming.

http://www.bkjia.com/PHPjc/1133037.html www.bkjia.com true http://www.bkjia.com/PHPjc/1133037.html techarticle In- depth understanding of the opcode principles of PHP, in-depth understanding opcode This article explains the principles of opcode in PHP. Share to everyone for your reference, as follows: OpCode is a php script compilation ...

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