This article illustrates the principle of opcode in PHP. Share to everyone for your reference, specific as follows:
OpCode is a PHP script-compiled intermediate language, like the Java bytecode, or. Net of the MSL. This article is mainly based on the "Understanding OPcode" and the network, according to individual understanding and modification, Special records:
PHP Code:
<?php
echo "Hello World";
$a = 1 + 1;
echo $a;
? >
PHP executes this code through the following 4 steps:
1. Scanning (lexing), convert the PHP code into a language fragment (tokens)
2. Parsing, converts tokens into simple and meaningful expressions
3. Compilation, compile the expression into Opocdes
4. Execution, execute opcodes in sequence, one at a time, so as to realize the function of PHP script.
Note: Now some cache 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 of all, ZEND/ZEND_LANGUAGE_SCANNER.C will be based on the ZEND/ZEND_LANGUAGE_SCANNER.L (lex file) to the input of the PHP code lexical analysis, so as to get a "word", php4.2+ Started to provide a function called Token_get_all, this function can tell a section of PHP code scanning into tokens;
<?php
$tokens = Token_get_all (' <?php
echo ' Hello World ');
$a = 1 + 1;
echo $a;
? > ');
Print_r ($tokens);
? >
The following results will be obtained:
Array ([0] => Array ([0] => 367 [1] => <?php [2] => 1) [1] => array ([0] => 370 [1] => [2] => 2) [2] => Array ([0] => 316 [1] => ; echo [2] => 2) [3] => Array ([0] => 370 [1] => [2] => 2) [4] =&G T
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] => = [ten] => Array ([0] => 370 [1] => [2] => 3) [one] => A
Rray ([0] => 305 [1] => 1 [2] => 3) [[] => Array ([0] => 370 [1] => [2] => 3) [=> + [] => array ([0] => 370 [1] => [2] => 3) [A] => array (
[0] => 305 [1] => 1 [2] => 3) [] =>; [] => Array ([0] => 370 [1] => [2] => 3) [A] => array ([0] => ; 316 [1] => echo [2] => 4) [to] => Array ([0] => 370 [1] => [2] =& Gt
4) => Array ([0] => 309 [1] => $a [2] => 4) [] =>; [A] => array ([0] => 370 [1] => [2] => 4) [] => Array ([0] => ;
369 [1] =>?> [2] => 5))
Return the result, the source code in the string, character, space, will be returned as is. 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
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 phase, it will compile tokens into Op_array, each op_arrayd contains the following 5 parts:
1. The identification of the opcode number, 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, the PHP code will be parsing:
[Root@localhost html]#/usr/local/php/bin/php-dvld.active=1 hello.php
Branch analysis from position:0
return Found
filename: /var/www/html/hello.php
function name: (NULL) Number of
ops:6
compiled vars:!0 = $a
Line # OP fetch ext. 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_exception
Hello World2
Each operand consists 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, holds the value of this operand (const) or the left value (Var) in different types, depending on the op_type.
And for Var, each var is different. 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, No. 0 Unknown temporary variable is_var of the variable table this is our general variable, and they begin with the $ IS_CV represents a cache mechanism used by the ze2.1/php5.1 compiler, which holds the address of the variable it references, 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.
The $a variable is optimized to be! 0.
More interested in PHP related content readers can view the site topics: "PHP Math Arithmetic Skills summary", "PHP operation Office Document Skills Summary (including word,excel,access,ppt)", "PHP Array" operation Skills Encyclopedia, " Summary of the PHP sorting algorithm, "PHP commonly used 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 (String) Usage summary" and " A summary of common PHP database operations tips
I hope this article will help you with the PHP program design.