"PHP Learning Notes" ticks article

Source: Internet
Author: User
Tags message queue

1. What is Ticks


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

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 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 the

As for what is low-level statements, do not start here, in summary, low-level statements includes the following situations:
(1) Simple statement: empty statement (one; number), return, break, continue, throw, Goto, Global, Static, unset, Echo, built-in HTML text, semicolon-terminated expression, etc. all count as one 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

It is important to note that: Declare () is not a function!!! To be precise, he is talking about a language structure, so there may be some unexpected behavior, for example, when you use declare () in a file many times, the principle of parsing is: Who is in front of me and who I am using recently, completely ignoring your code logic, do not unfold here. One of the suggested uses 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:


We found that the tick event was triggered every two statements, due to the registration of the Tick event's callback, thus performing an action to cancel the coupon from the message queue. This simulates the process of sending and receiving messages.




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.