Home >Backend Development >PHP Tutorial >Zend opcode是如何被执行的?是要编译为机器码再执行吗?
php先被Zend引擎编译为opcode,那么opcode又是如何被zend_execute执行的呢?最终是被编译为机器码了吗?具体过程是如何的?
<code class="language-c"><span class="k">while</span> <span class="p">(</span><span class="n">TRUE</span><span class="p">)</span> <span class="p">{</span>
<span class="kt">int</span> <span class="n">opcode</span> <span class="o">=</span> <span class="o">*</span><span class="n">program_counter</span><span class="p">;</span>
<span class="k">switch</span> <span class="p">(</span><span class="n">opcode</span><span class="p">)</span> <span class="p">{</span>
<span class="k">case</span> <span class="nl">ZEND_ADD</span><span class="p">:</span>
<span class="c1">// execute add ...</span>
<span class="n">program_counter</span><span class="o">++</span><span class="p">;</span> <span class="c1">// next opcode</span>
<span class="k">break</span><span class="p">;</span>
<span class="k">case</span> <span class="nl">ZEND_SUB</span><span class="p">:</span>
<span class="c1">// execute sub ...</span>
<span class="n">program_counter</span><span class="o">++</span><span class="p">;</span> <span class="c1">// next opcode</span>
<span class="k">break</span><span class="p">;</span>
<span class="c1">// ...</span>
<span class="p">}</span>
<span class="p">}</span>
</code>
PHP源码 ==(interpreter)==> opcode(PHP: Zend Engine 2 Opcodes,理解成汇编) ==(zend engine)==> 机器码
计算机只能执行机器码,但是并不是把PHP代码直接转为机器码执行,而是转化为一条条的opcode,其实opcode对应的是一个C函数,执行一条opcode就是执行这条opcode对应的C函数,而这个C函数已经被编译为机器码了