Analysis of PHP opcode through VLD Extension

Source: Internet
Author: User
Tags echo command

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.