Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

An example Analysis of the execution cycle of PHP principle

2025-04-07 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly shows you the "example analysis of the implementation cycle of the PHP principle", the content is simple and clear, and I hope it can help you solve your doubts. Let the editor lead you to study and study the "example analysis of the implementation cycle of the PHP principle".

The details are as follows:

The execution cycle of PHP can be divided into the following stages, from the initial PHP script we wrote-> to the final script being executed-> to get the execution result:

First of all, Zend Engine (ZE), call the lexical analyzer (generated by Lex, the source file is in Zend/zend_language_sanner.l), we want to execute the PHP source file, remove spaces, comments, split into a token.

Then, ZE will give the resulting token forward to the parser (yacc, the source file is in Zend/zend_language_parser.y), generating an opcode,opcode that usually exists in the form of op array, which is the intermediate language executed by PHP.

Finally, ZE calls zend_executor to execute the op array, outputting the result. (that is, convert the source file to the machine language, and then run it on the virtual machine. )

ZE is a virtual machine, and it is precisely because of its existence that we can write PHP scripts without considering the type of operating system. This is the reason for the portability of PHP. ZE is a CISC (complex instruction processor) that supports 150 instructions (specific instructions in Zend/zend_vm_opcodes.h), from the simplest ZEND_ECHO (echo) to complex ZEND_INCLUDE_OR_EVAL (include,require). All the PHP we write will eventually be processed into a sequence of these 150 instructions (opcode) and finally executed.

PHP is a scripting language, that is to say, the PHP code written by the user will eventually be interpreted and executed by the PHP interpreter, and all the written PHP code will be translated into virtual instructions (OPCODES) of PHP's virtual machine ZE.

So what does our PHP script end up being translated into? In other words, what does op code look like? Opcode is an intermediate language for compiling PHP scripts.

There are already such modules in PECL, using the VLD (Vulcan Logic Dissassembler) module developed by Derick Rethans. Just download the module and load it into PHP, and you can get the result of the script translation through simple settings.

Installation and application of VLD module:

[root@localhost software] # tar zxvf vld-0.9.1.tgz.gz [root@localhost vld-0.9.1] # / usr/local/php/bin/phpize [root@localhost vld-0.9.1] #. / configure-- with-php-config=/usr/local/php/bin/php-config [root@localhost vld-0.9.1] # make install / / No make is required

Edit the php.ini file and activate the vld extension.

Example:

Create a file, such as hello.php

Execute:

[root@localhost html] # / usr/local/php/bin/php-dvld.active=1 hello.phpBranch analysis from position: 0Return foundfilename: / var/www/html/hello.phpfunction name: (null) number of ops: 3compiled vars: noneline # op fetch ext return operands -- 20 ECHO 'hello%2C+world.' 4 1 RETURN 1 2 * ZEND_HANDLE_EXCEPTIONhello World.

Look at the other one:

[root@localhost html] # vi vld.php

Execute:

[root@localhost html] # / usr/local/php/bin/php-dvld.active=1 vld.phpBranch analysis from position: 0Return foundfilename: / var/www/html/vld.phpfunction name: (null) number of ops: 5compiled vars:! 0 = $iline # op fetch ext return operands -- 30 ASSIGN! 0 'This+is+a+string' 7 1 CONCAT ~ 1! 0,' + that+has+been+echoed+on+screen' 2 ECHO ~ 1 103 RETURN 1 4 * ZEND_HANDLE_EXCEPTIONThis is a string that has been echoed on screen

Note: ZEND_HANDLE_EXCEPTION is the 149th instruction in Zend/zend_vm_opcodes.h

Compiled vars:! 0 = $I here is the variable that gets the variable name "I" at! 0 (* zval).

# 0 assign (ASSIGN) the string "this+is+a+string" to! 0

# 1 string connection

# 2 display

This intermediate code is executed directly by the Zend VM (Zend virtual machine). The function that is really responsible for execution is: zend_execute (zend_execute.h)

The above is all the contents of the article "sample Analysis of the execution cycle of PHP principles". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report