**一.opcodes作用** opcodes是一种php脚本编译后的中间语言,就像java的bytecode,或者.net的msl **二.原生php代码** ~~~ <?php echo "Hello World"; $a = 1 + 1; echo $a; ?> ~~~ **三.PHP执行这段代码会经过如下4个步骤** ~~~ #PHP的语言引擎Zend 1.Scanning(Lexing) ,将PHP代码转换为语言片段(Tokens) 2.Parsing, 将Tokens转换成简单而有意义的表达式 3.Compilation, 将表达式编译成Opocdes 4.Execution, 顺次执行Opcodes,每次一条,从而实现PHP脚本的功能 ~~~ **四.Scanning** ~~~ #用token_get_all处理以上PHP原生代码 #index.php <?php $str='<?php echo "Hello World"; $a = 1 + 1; echo $a; ?>'; $arr=$tokens=token_get_all($str); echo "<pre>"; print_r($arr); echo "</pre>"; ?> ~~~ ~~~ 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] => ) [10] => + [11] => Array ( [0] => 370 [1] => ) [12] => Array ( [0] => 305 [1] => 1 ) [13] => ; [14] => Array ( [0] => 370 [1] => ) [15] => Array ( [0] => 316 [1] => echo ) [16] => Array ( [0] => 370 [1] => ) [17] => ; ) ~~~ 2.Parsing #接下来,就是Parsing阶段了,Parsing首先会丢弃Tokens Array中的多余的空格,将剩下的转成一个一个的简单的表达式 ~~~ 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 ~~~ 3.Compilation ~~~ #它会把Tokens编译成一个个op_array * ZEND_ECHO 'Hello World' * ZEND_ADD ~0 1 1 * ZEND_ASSIGN !0 ~0 * ZEND_ECHO !0 > > ~~~ 4.由Zend引擎一行一行读取并执行.