The usage of tick in PHP control Structure statement declare

Source: Internet
Author: User
The declare structure is used to set execution instructions for a piece of code. The syntax of declare is similar to other process Control structures:

The directive section allows you to set the behavior of the declare code snippet. Currently only two commands are known: ticks (see ticks instructions below for more information) and encoding (see encoding instructions below for more information).

Note:

The ticks directive is obsolete in PHP 5.3.0 and will be removed from PHP 6.0.0.

Encoding is a new directive for PHP 5.3.0.

Tick is an event that occurs every time an interpreter executes N low-level statements in a declare code snippet. The value of N is specified in the directive section of declare with Ticks=n.

The events that appear in each tick are specified by Register_tick_function (). See the example below for more details. Note Multiple events can occur in each tick.

Read the manual or feel foggy, and then see how others describe:

Based on code parsing:

<?php        function doticks ()        {            echo ' Ticks ';        }        Register_tick_function (' doticks ');        DECLARE (ticks = 1) {for            ($x = 1; $x < + + + $x) {                echo $x * $x. ' <br/> ';            }     ? >

Result of Operation:

1    TicksTicks4    TicksTicks9    TicksTicks16    TicksTicks25    TicksTicks36    TicksTicks49    TicksTicks64    TicksTicks81    ticksticksticksticks

Generate three questions:

(1) Why output "Ticks" before outputting 1?

(2) Why the output of four ticks after 81?

(3) How does the For loop in declare break down into a low-level statement (low-level)?

This is the problem that every person who first touches ticks will encounter. First, the Register_tick_function function defines the handler function at the time of each tick event. So what is the Tick event?

There are three layers of meaning:

(1) Tick is an event.

(2) The Tick event occurs once in PHP for each n low-level statement, and N is specified by the Declare statement.

(3) You can use Register_tick_function () to specify what action should be taken when the tick event occurs.

Clearly, the key to understanding the output above is to understand what a low-level statement (low-level statements) is, and how it counts. We'll start by compiling the above program into opcodes via Opdump:

It is clear that the PHP compilation process has inserted the ticks instruction in the opcode sequence of each statement to handle the Tick event after compilation. So what are the rules for these ticks to be inserted?

We're still looking for answers from the source code of PHP Zend engine.

With a simple text search we can know that the only function that generates zend_ticks instructions is zend_do_ticks (the implementation of this function is in zend_compile.c).

Now from the PHP parsing file zend_language_parser.y (PHP uses Bison to do parsing, all the grammar rules are defined in ZEND_LANGUAGE_PARSER.Y) to find the place to call Zend_do_ticks.

Once again, using a simple text search, we can get the three syntax rules that call Zend_do_ticks:

Statement:    unticked_statement {zend_do_ticks (Tsrmls_c);}     | ...     ;    Function_declaration_statement:    unticked_function_declaration_statement {zend_do_ticks (TSRMLS_C);}     ;    Class_declaration_statement:    unticked_class_declaration_statement {zend_do_ticks (TSRMLS_C);}     ;

That is, PHP compilation will be in statement (statement), function_declaration_statement (function definition statement), class_declaration_statement after inserting ticks handler function, That is, it inserts a ticks instruction after each statement, function declaration, class (actually including interface) declaration.

A function is better understood than a class declaration statement, as defined by the syntax of unticked_function_declaration_statement:

Unticked_function_declaration_statement:    function is_reference t_string {zend_do_begin_function_declaration ( &$1, &$3, 0, $2.op_type, NULL tsrmls_cc); }     ' (' parameter_list ') ' {' inner_statement_list '} ' {zend_do_end_function_declaration (&$1TSRMLS_CC);}    ;

It is known that function_declaration_statement is a complete function declaration, that is, the definition of a function includes the complete function prototype and the function body to calculate a function_declaration_statement.

Related Article

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.