Reputation: 6939
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)
Upvotes: 0
Views: 274
Reputation: 21
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.
Upvotes: 0
Reputation: 17289
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
Upvotes: 1