Jump to content

register_tick_function() and declare(ticks=...)


Go to solution Solved by requinix,

Recommended Posts

https://www.php.net/manual/it/function.register-tick-function.php

Specifically

https://www.php.net/manual/it/control-structures.declare.php#control-structures.declare.ticks

Quote

A tick is an event that occurs for every N low-level tickable 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.

Honestly, it's not very clear.
Can someone explain it better?

Edited by rick645
Link to comment
https://forums.phpfreaks.com/topic/329083-register_tick_function-and-declareticks/
Share on other sites

  • rick645 changed the title to register_tick_function() and declare(ticks=...)

A tick happens every time the engine does something at a fairly granular level. Like executes a statement, but even lower-level than that.

The easiest way to understand it is going to be to play with code: set up a ticket handler every 1/2/3/whatever ticks, have it output something, and then write a bunch of code to execute and see what happens.

Posted (edited)
On 6/28/2025 at 11:20 AM, requinix said:

A tick happens every time the engine does something at a fairly granular level.

Granular level?

Quote

Like executes a statement, but even lower-level than that.

 

????

I don't understand

????

Quote

The easiest way to understand it is going to be to play with code: set up a ticket handler every 1/2/3/whatever ticks, have it output something, and then write a bunch of code to execute and see what happens.

 

$ tree
├── tick1.php
├── tick2.php
└── tick3.php

$ cat tick1.php 
<?php declare(ticks=1);
function my_tick_function() {
    debug_print_backtrace();
}
register_tick_function('my_tick_function');

$ cat tick2.php 
<?php declare(ticks=2);
function my_tick_function() {
    debug_print_backtrace();
}
register_tick_function('my_tick_function');

$ cat tick3.php 
<?php declare(ticks=3);
function my_tick_function() {
    debug_print_backtrace();
}
register_tick_function('my_tick_function');

$ php tick1.php 
#0 /tmp/tmp.MLb8GRwmkU/tick1.php(5): my_tick_function()
$ php tick2.php 
#0 /tmp/tmp.MLb8GRwmkU/tick2.php(5): my_tick_function()
$ php tick3.php 
$

Why the php tick3.php command does not produce output?

Edited by rick645
  • Solution
2 hours ago, rick645 said:

Granular level?

Yes, granular, as in "highly detailed; having many small and distinct parts".

2 hours ago, rick645 said:

Why the php tick3.php command does not produce output?

Because you missed the part in my reply where I said "write a bunch of code". You wrote a very small amount and you're not going to see how ticks work unless you write a lot more.

$ cat tick3.php 
<?php declare(ticks=3);
function my_tick_function() {
    debug_print_backtrace();
}
register_tick_function('my_tick_function');
abs(1); // added
abs(1); // added
abs(1); // added


		
$ php tick3.php 
#0 /tmp/tmp.MLb8GRwmkU/tick3.php(6): my_tick_function()
#0 /tmp/tmp.MLb8GRwmkU/tick3.php(8): my_tick_function()

OK, thanks

One thing that might be helpful is to use the declare to wrap the block of code you want to have evaluated for statement processing.

 

$count = 0;

function statements() {
  global $count;
  $count++;
  echo "Statement Count: $count\n";
}

register_tick_function('statements');

declare(ticks=5) {
    for ($x = 0; $x < 10; $x++) {
        echo "\$x = $x \n";
    }
}

And you get:

$x = 0 
$x = 1 
$x = 2 
$x = 3 
$x = 4 
Statement Count: 1
$x = 5 
$x = 6 
$x = 7 
$x = 8 
$x = 9 
Statement Count: 2

 

  • Great Answer 1

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.