yangakw yangakw

php原理之opcode

in 代码read (56) 文章转载请注明来源!

看鸟哥博客
http://www.laruence.com/2008/06/18/221.html
了解下。
php是一种脚本语言,脚本是需要解析成机器语言才能够运行,php的运行原理大致可以分为:
1.词法解析(把代码中的空格注释去掉,生成tokens)
2.语言解析(将tokens转化成有意义的表达式,生成opcode)
3.执行机器码(由ZE[Zend Engine]执行opcode)
通过上面的流程可以发现,其实这个过程是一个重复的过程,缓存OPCODE是个不错的选择,所以有了php的apc(alternative php cache).
详细说下php执行流程,在词法解析的时候可以通过函数 token_get_all 查看解析的结果。

         <?php
           echo "Hello World";
           $a = 1 + 1;
           echo $a;
        ?>
        代码会解析成
        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] => ;
        )

jrotty WeChat Pay

微信打赏

jrotty Alipay

支付宝打赏

文章二维码

扫描二维码,在手机上阅读!

发表新评论
博客已运行
© 2017 yangakw
PREVIOUS NEXT
雷姆
拉姆