The machine can only understand the machine language, but how is Php executed by the machine as an interpreted script?
In fact, PHP is divided into lexical analysis, syntax analysis, and opcode compiling during execution. The Zend engine will execute these Opcodes.
During the above execution, the performance problem of interpreted language is often raised because the above process will be repeated every time the script is executed. Therefore, opchode caches such as APC, xcache, and eaccelerator appear.
1. Install VLD extension (Linux)
1. Download
Address: http://pecl.php.net/package/vld
2. Unzip and install
# tar zxvf vld-0.11.1.tgz# cd ./vld-0.11.1# /usr/local/php/bin/phpize # ./configure --with-php-config=/usr/local/php/bin/php-config --enable-vld# make && make install
Here, for download is the vld-0.11.1.tgz and my PHP path is/usr/local/PHP
3. Modify PHP. ini
Extension = VLD. So
4. Restart Apache or PHP-FPM.
2. Let's look at opcode through a simple example.
PHP code:
<?phpecho "hello world\n";$str = "strtest";$str2 = "jinyong";print strlen($str);echo strlen($str2);
Opcode:
What information can you obtain?
Line number, command number, script start mark, end mark, Zend VM command, return value, Zend VM command parameters.
3. Run opcode on Zend VM
Struct _ zend_op {opcode_handler_t handler; // znode result called when the opcode is executed; znode OP1; znode OP2; ulong extended_value; uint lineno; zend_uchar opcode; // opcode code}; struct _ operator {/* Common Elements */zend_uchar type; char * function_name; operator * scope; zend_uint fn_flags; Union _ zend_function * prototype; zend_uint num_args; zend_uint limit; optional * arg_info; zend_bool limit; unsigned char return_reference;/* end of common elements */zend_bool limit; zend_uint * refcount; zend_op * Opcodes; limit last, size; optional * vars; int last_var, size_var; zend_uint t; optional * brk_cont_array; int comment; int current_brk_cont; optional * comment; int comment;/* Static variables support */hashtable * static_variables; zend_op * start_op; int backpatch_count; zend_uint this_var; char * filename; zend_uint line_start; zend_uint line_end; char * doc_comment; zend_uint begin; zend_uint early_binding; /* The linked list of delayed declarations */void * Reserved [zend_max_reserved_resources];}; zend_api void execute (zend_op_array * op_array tsrmls_dc ){//... execute opcode in op_array cyclically or execute opcode in other op_array}
In fact, the PHP we wrote is finally parsed into the instruction set in Zend Vm, and finally the result is returned through Zend VM.
Each command can find the function to execute. For example, the echo command corresponds to zend_do_echo. For more information, see Zend/compile. h.
For more instructions, see http://php.net/manual/en/internals2.opcodes.list.php. The cases of each instruction are listed at the same time.