ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
**一.opcodes作用** opcodes是一种php脚本编译后的中间语言,就像java的bytecode,或者.net的msl &emsp; **二.原生php代码** ``` <?php echo "Hello World"; $a = 1 + 1; echo $a; ?> ``` &emsp; **三.PHP执行这段代码会经过如下4个步骤** ``` #PHP的语言引擎Zend 1.Scanning(Lexing) ,将PHP代码转换为语言片段(Tokens) 2.Parsing, 将Tokens转换成简单而有意义的表达式 3.Compilation, 将表达式编译成Opocdes 4.Execution, 顺次执行Opcodes,每次一条,从而实现PHP脚本的功能 ``` &emsp; **四.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] => ; ) ``` &emsp; 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 ``` &emsp; 3.Compilation ``` #它会把Tokens编译成一个个op_array * ZEND_ECHO 'Hello World' * ZEND_ADD ~0 1 1 * ZEND_ASSIGN !0 ~0 * ZEND_ECHO !0 > > ``` 4.由Zend引擎一行一行读取并执行. &emsp; ###**系统的学习PHP** 关注:PHP自学中心,回复相应的关键词,领取以下视频教程 **9 PHP异步通信框架Swoole实战** 公众号里回复:08250024 &emsp; #### **还有其他的教程的关键词,请关注公众号查看每天分享的文章教程的头部** ![](https://img.kancloud.cn/96/af/96af322d2cdc53d3fbbe981affa60c7f_150x150.jpg)