PHP has no timer?


Indeed, PHP does not have native timer related functions like setinterval or setTimeout in JS. But we can do it in other ways, such as using declare.

Let’s see how it is implemented, and then we’ll learn what the declare expression is.

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

declare (ticks = 1) {
    while (1) {
        sleep(1); //  Here, do is called every time it is executed_ tick()

Very simple code, after running, will output the current time per second.

Declare syntax is defined as follows:

declare (directive)
  • The declare structure is used to set the execution instructions of a piece of code
  • The directive section allows you to set the behavior of the declare snippet. At present, only two instructions are known: ticks and encoding
  • Tick (clock cycle) is an event that occurs every n timed low-level statements executed by the interpreter in the declare code segment. The value of n is specified with ticks = n in the directive part of declare
  • The events that occur in each tick are controlled by register_ tick_ Function()

Here, we only study the use of ticks.

In the above code, we use register_ tick_ Function() registers do_ The tick () method specifies ticks = 1 for ticks and declare, that is, register will be executed every time a timed low-level statement is executed_ tick_ Method registered in function(). Every time the while in the declare code block loops, a sleep () 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, both conditional expressions and parameter expressions are not timeable.

Let’s take a look at the step-by-step implementation of declare through the following example:

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

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

    $j++; // test_tick: 2
    sleep(1); //   After stopping for 1 second, test_ tick:3

    $j++; // test_tick:4

    If ($J = = 3) {// conditional expression, does not execute ticks

        echo "aa", PHP_ EOL; //  test_ tick:5 \n   test_ tick:6,PHP_ EOL accounting once ticks

//Declare uses curly braces. All codes behind them have no effect, and the scope is limited to curly braces
echo "bbb"; // 
echo "ccc"; // 
echo "ddd"; //

The notes are very detailed, so we don’t have to explain them one by one. Let’s look at the result of setting ticks to 2 and declaring the following statement without curly braces:

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

$j = 0; //  No timing here
declare (ticks = 2); 
$j++; // test_tick1:0 


sleep(1); //   Test after stopping for 1 second_ tick1:1


$j++; // test_tick1:2

If ($J = = 4) {// conditional expression, does not execute ticks
    // echo "aa", PHP_EOL;
    echo "aa"; //  test_ tick:10,test_ Tick 1 does not execute and does not skip two steps. If it is used, PHP_ EOL, then count two steps, and test will be output_ tick1:3

//If declare does not use curly braces, it will have an effect on all subsequent codes. If it is require or include, it will not process the subsequent contents of the parent page
echo "bbb"; // test_tick1:3
echo "ccc";
echo "ddd"; // test_tick1:4

It can be seen that we declare has an effect on the subsequent code of its definition, but it should be noted that if there is page nesting, it has no effect on the subsequent code of the parent page. Once ticks = 2 is defined, register will be executed once after two low-level timeable codes_ tick_ Function() registered function code.

Test code:

Reference documents:


Official account: hard core project manager

Add wechat / QQ friends: [xiaoyuezigonggong / 149844827] get free PHP and project management learning materials

Tiktok, official account, voice, headline search, hard core project manager.

Station B ID: 482780532