Deeply understand the execution cycle analysis of PHP principles, and deeply understand php
This article describes the execution cycle of PHP principles. We will share this with you for your reference. The details are as follows:
The execution cycle of PHP can be divided into the following stages:
First, Zend Engine (ZE) calls the lexical analyzer (generated by Lex, the source file is in Zend/zend_language_sanner.l) and removes spaces and comments from the PHP source file to be executed, split into tokens.
Then, ZE sends the obtained token forward to the syntax analyzer (yacc is generated, and the source file is in Zend/zend_language_parser.y) to generate an opcode one by one. The opcode usually exists in the op array format, it is an intermediate language for PHP Execution.
Finally, ZE calls zend_executor to execute op array and output the result. (That is, convert the source file to the machine language and run it on the Virtual Machine .)
ZE is a virtual machine. It is because of its existence that we can write PHP scripts without considering the operating system type. This is the reason for PHP portability. ZE is a CISC (complex instruction processor) that supports 150 commands (the specific commands are in Zend/zend_vm_opcodes.h), from the simplest ZEND_ECHO (echo) to the complex ZEND_INCLUDE_OR_EVAL (include, require ),All PHP code we write will eventually be processed as the sequence of these 150 commands (op code), and then 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, the virtual commands (OPCODES) translated into PHP virtual machines are executed.
What is our PHP script translated? That is to say, what does the op code look like? Opcode is an intermediate language after PHP script compilation.
This module is already available in PECL, using the Vulcan Logic Dissassembler developed by Derick Rethans. You just need to download this module and load it into PHP, you can get the script translation result through simple settings.
Installation and application of the 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 // do not need make
Edit the php. ini file and activate the vld extension.
Instance:
Create a file, such as hello. php.
<?php echo 'hello, world.';?>
Run:
[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------------------------------------------------------------------------------- 2 0 ECHO 'hello%2C+world.' 4 1 RETURN 1 2* ZEND_HANDLE_EXCEPTIONhello, world.
Look at another one:
[root@localhost html]# vi vld.php<?php $i = "This is a string"; //I am comments echo $i. ' that has been echoed on screen';?>
Run:
[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------------------------------------------------------------------------------- 3 0 ASSIGN !0, 'This+is+a+string' 7 1 CONCAT ~1 !0, '+that+has+been+echoed+on+screen' 2 ECHO ~1 10 3 RETURN 1 4* ZEND_HANDLE_EXCEPTIONThis is a string that has been echoed on screen
Note: ZEND_HANDLE_EXCEPTION is the first command in Zend/zend_vm_opcodes.h.
Compiled vars :! 0 = $ I here is the variable for obtaining the variable name "I" on! 0 (* zval ).
#0 ASSIGN the "this + is + a + string" value (ASSIGN) to the string! 0
#1 string connection
#2 display
The intermediate code is directly executed by Zend VM. The real execution function is zend_execute (zend_execute.h ).