The register_tick_function
The register_tick_function function allows you to register a function,
which will be called on every tick of script execution. The first parameter
takes the function name or an anonymous function, subsequent parameters are arguments
for the passed function. For the function to work, ticks must be enabled using
declare(ticks=N), where N is the number of ticks between calls.
Syntax
register_tick_function(callable $function, mixed ...$args);
Example
A simple example of registering a function for execution on each tick:
<?php
declare(ticks=1);
function tick_handler() {
echo "Tick executed\n";
}
register_tick_function('tick_handler');
$a = 1;
$b = 2;
$c = $a + $b;
?>
Execution result:
Tick executed
Tick executed
Tick executed
Tick executed
Example
Using an anonymous function with parameters:
<?php
declare(ticks=2);
register_tick_function(function($msg) {
echo $msg . "\n";
}, "Tick!");
for ($i = 0; $i < 5; $i++) {
// Some code
}
?>
Execution result:
Tick!
Tick!
Example
Unregistering a function using unregister_tick_function:
<?php
declare(ticks=1);
function tick_log() {
echo date('H:i:s') . "\n";
}
register_tick_function('tick_log');
// First part of the code
$a = 10;
$b = 20;
unregister_tick_function('tick_log');
// Second part of the code
$c = $a + $b;
?>
Execution result:
14:25:03
14:25:03
See Also
-
the
unregister_tick_functionfunction,
which unregisters a function for execution on ticks