"PHP Learning Notes" ticks article

Source: Internet
Author: User

1. What is Ticks


Let's take a look at the explanation of the ticks on the brochure:

A Tick is an event, occurs for every N low-level statements executed by the parser within the declare block. The value for N is specified using Ticks=n within the Declare block ' s Directive section.


To summarize:

    • Tick is an event
    • The Tick event is released once per run n low-level statements, n is specified by the Declare statement
    • Register_tick_function () can be used to specify the time of the Handler,unregister_tick_function () corresponding to it

As for what is low-level statements. Do not expand here, in summary, low-level statements contains the following situations:
(1) Simple statement: An empty statement (a. Number). Return, break, continue, throw, Goto, Global, Static, unset, Echo, built-in HTML text. An expression that ends with a semicolon is counted as a statement. (2) Compound statement: Complete If, ElseIf, while, Do...while, for, foreach, switch, try...catch, etc to calculate a statement(3) statement block: {} braces count a block of statements(4) Declare itself is a compound statement

All the statement, function_declare_statement, class_declare_statement constitute the low-level statement.


2. Tick Pits

One thing to note: Declare () is not a function!! To be exact, he is talking about a language structure. So there may be some unexpected behavior. For example, when you use declare () multiple times in a file, the principle of parsing it is: Who is in front of me and who I am using recently, ignoring your code logic completely. Do not expand here. One recommended way to use this is

Declare (ticks=10) {for    ($i = 0; $i <; $i + +) {        print "hello\n";    }} Declare (ticks=2) {for    ($i = 0; $i <; $i + +) {        print "hello\n";    }}

3. Application of Tick

When we say so much, when are we going to use tick? In general, tick can be used for debugging, performance testing, simple multitasking or background I/O operations, and so on.

Here, give me a bird. Example provided for complete communication

<?php/* * Use ticks to complete message communication *///create a message Queue$mesg_key = Ftok (__file__, ' m '); $MESG _id = Msg_get_queue ($MESG _key, 0666)//ticks callbackfunction fetchmessage ($mesg _id) {    if (!is_resource ($MESG _id)) {        Print_r ("Mesg Queue is Not ready \ n ");    }    if (msg_receive ($MESG _id, 0, $MESG _type, 1024x768, $MESG, False, msg_ipc_nowait)) {        Print_r ("Process got a new incoming MS G: $MESG \ n ");    }} Register Ticks Callbackregister_tick_function ("Fetchmessage", $mesg _id);//send messages;declare (ticks = 2) {    $i = 0;    while (+ + $i <) {        if ($i% 5 = = 0) {            msg_send ($mesg _id, 1, "Hi:now Index is:". $i);}}}    


Let's take a look at the output:

watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvdhvhbnr1yw5scw==/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/southeast ">

We found that the tick event was triggered every two statements because of the callback of the Tick event. The operation that cancels the interest from the message queue is thus run. This simulates the process of sending and receiving messages.




"PHP Learning Notes" ticks 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.