0

I know what ticks are in PHP, but looking at the output of the following code:

<?php

function myfunc() {
    static $n = 1;
    print "$n) Tick<br>";
    $n++;
}

register_tick_function("myfunc");

declare(ticks=1);
    echo 'echo<br>';

The output is:

1) Tick
2) Tick
echo
3) Tick

It tells me that the registered tick function 'myfunc' is executed 3 times. But, based on this answer -> PHP using Declare ? What is a tick?:

You get a tick for each line ; and each block {}

Shouldn't it be:

1) Tick
echo
2) Tick

? As there are only two statements:

declare(ticks=1);<-- Statement 1
        echo 'echo<br>';<-- Statement 2

Why 3??? If I remove the ";" from declare, like this:

declare(ticks=1)
        echo 'echo<br>';

I get the only one execution of the registered tick function:

echo
1) Tick

So what is the definitely rule to count the tickable statements in order to understand how many times a registered tick function is executed? (I am asking it because of this example and because PHP manual actually doesn't cover the topic on counting tickable stats)

EDIT: Another strange behaviour in my opinion is this:

<?php

function myfunc()
{
    static $n = 1;
    print "$n) Tick<br>";
    $n++;
}

register_tick_function("myfunc");
declare(ticks = 1)
    echo 'Start<br>';
    echo 'echo<br>';

which outputs:

Start
1) Tick
echo

The tick function is executed once, but the statements are at least 2 (if not counting the "end of the script" as @Marc B has pointed out)

Community
  • 1
  • 1
tonix
  • 6,671
  • 13
  • 75
  • 136
  • there's also the implicit tick of the "end of script", which is kinda-sorta like a `}`. – Marc B Jan 06 '15 at 20:20
  • All right that should make sense, but then why if I have `declare(ticks=1) echo 'echo
    ';` I only get the tick handler function executed only once (for the `echo` statement) and not for the "end of script"?
    – tonix Jan 06 '15 at 20:56
  • 1
    I'll guess that `declare echo;` is a single statement, since there's no `;`, so the tick enablement won't start until AFTER the statement is complete (after the echo is performed), so the single tick you get is the end-of-script, not the tick from the echo. – Marc B Jan 06 '15 at 21:33
  • Even this could make sense, but then if you say that `declare` is not counted as, in the first script where the tick fun is executed 3 times (one for declare, echo and "end of the script") it should execute only 2 (not for the declare)... – tonix Jan 07 '15 at 09:54

2 Answers2

1

what I finelly found is:

function myfunc()
    {
        static $n = 1;
        print "$n) Tick<br>";
        $n++;
    }

    register_tick_function("myfunc");
    declare(ticks = 1) {

    //echo 'Start<br>';
    echo 'echo<br>';

    }

outputs 2 ticks, one for block {} and 1 for echo. if you uncomment 'Start' that will bring 1 more tick as you expected.

So I think the best practice is to always use

declare(ticks=1) { } 

with block brackets

Alex
  • 16,739
  • 1
  • 28
  • 51
0

You don't put a semicolon after the declare, so your declare statement works only for the next statement (for one only echo). It is the same behaviour, as with using a block in curly brackets after declare - that block is then regarded as the only statement to execute. You have the same with control structures: while(true)x(); and while(true){x();y();}, just in the case with declare semicolon after it creates an implicit block around all the remaining script.

Miguel
  • 11
  • 2