Home  >  Article  >  Backend Development  >  How to implement timer effect in PHP

How to implement timer effect in PHP

醉折花枝作酒筹
醉折花枝作酒筹forward
2021-06-25 16:03:432857browse

PHP does not have native timer-related functions similar to setInterval or setTimeout in JS. But we can do it in other ways, such as using declare. Let me introduce the usage of declare to you.

How to implement timer effect in PHP

Let’s first take a look at how it is implemented, and then let’s learn what the declare expression is. .

function do_tick($str = '')
{
    list($sec, $usec) = explode(' ', microtime());
    printf("[%.4f] Tick.%s\n", $sec + $usec, $str);
}
register_tick_function('do_tick');

do_tick('--start--');
declare (ticks = 1) {
    while (1) {
        sleep(1); // 这里,每执行一次就去调用一次do_tick()
    }
}

This is a very simple code that will output the current time every second after running.

declare syntax is defined as follows:

declare (directive)
    statemaent;
  • declare structure is used to set the execution instruction of a piece of code

  • directive part Allows setting the behavior of declare snippets. Currently, only two instructions are known: ticks and encoding

  • Tick (clock cycle) is an event that occurs every time the interpreter executes N low-level statements that can be timed in the declare code segment. . The value of N is specified by ticks=N in the directive part of declare

  • The events that occur in each tick are specified by register_tick_function()

Here, we only study the use of ticks.

In the above code, we use register_tick_function() to register the do_tick() method for ticks, and declare specifies ticks=1, which means that every time a low-level statement that can be timed is executed, register_tick_function() will be executed. method of registration. When the while in the declare code block loops each time, there is a sleep() that pauses for one second, and this sleep() is the low-level statement that can be timed.

So, isn’t while() a low-level statement that can be timed? Of course not. Conditional judgments such as where and if are not such low-level statements that can be timed.

Not all statements can be timed. Usually conditional expressions and parameter expressions are not timeable.

Let’s take a look at how to execute the declaration step by step through the following example:

function test_tick()
{
    static $i = 0;
    echo 'test_tick:' . $i++, PHP_EOL;
}
register_tick_function('test_tick');
test_tick(); // test_tick:0

$j = 0; 
declare (ticks = 1) {
    $j++; // test_tick:1

    $j++; // test_tick: 2
    
    sleep(1); //  停1秒后,test_tick:3

    $j++; // test_tick:4

    if ($j == 3) { // 条件表达式,不会执行ticks

        echo "aa", PHP_EOL; // test_tick:5 \n   test_tick:6,PHP_EOL会计一次ticks
    }
}

// declare使用花括号后面所有代码无效果,作用域限定在花括号以内
echo "bbb"; // 
echo "ccc"; // 
echo "ddd"; //

The comments are very detailed, so we don’t need to explain them one by one. Let's look at the result of setting ticks to 2 and not using curly braces for the statemaent under declare:

function test_tick1() 
{
    static $i = 0;
    echo 'test_tick1:' . $i++, PHP_EOL;
}
register_tick_function('test_tick1');

$j = 0; // 此处不计时
declare (ticks = 2); 
$j++; // test_tick1:0 

$j++; 

sleep(1); //  停1秒后 test_tick1:1

$j++; 

$j++; // test_tick1:2

if ($j == 4) { // 条件表达式,不会执行ticks
    // echo "aa", PHP_EOL;
    echo "aa"; // test_tick:10,test_tick1不执行,没有跳两步,如果用了,PHP_EOL,那么算两步,会输出test_tick1:3
}

//  declare没有使用花括号将对后面所有代码起效果,如果是require或者include将不会对父页面后续内容进行处理
echo "bbb"; // test_tick1:3
echo "ccc";
echo "ddd"; // test_tick1:4

It can be seen that our declare has an effect on the subsequent code of its definition, but it should be noted that If there are nested pages, subsequent code on the parent page will have no effect. After defining ticks=2, the function code registered by register_tick_function() will be executed once after the two low-level timer codes.

Test code:

https://github.com/zhangyue0503/dev-blog/blob/master/php/201911/source/PHP%E6%B2%A1%E6%9C%89%E5%AE%9A%E6%97%B6%E5%99%A8%EF%BC%9F.php

Recommended learning: php video tutorial

The above is the detailed content of How to implement timer effect in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:segmentfault.com. If there is any infringement, please contact admin@php.cn delete