PHP Function Reference

PHP register_tick_function() Function



The PHP register_tick_function() function is used to register a function for execution when a tick is called.

Multiple calls to register_tick_function() can be made, and each will be called in the same order as they were registered. If exit() is called within one registered shutdown function, processing will stop completely and no other registered tick functions will be called.

Syntax

register_tick_function(callback, args)

Parameters

callback Required. Specify a function to register.
args Optional. Specify parameters to the tick function by passing additional parameters.

Return Value

Returns true on success or false on failure.

Example: register_tick_function() example

The example below shows the usage of register_tick_function() function.

<?php
declare(ticks=1);

//a function called on each tick event
function tick_handler() {
  echo "tick_handler() is called\n";
}

//registering a tick function
register_tick_function('tick_handler'); //causes a tick event

$a = 1;          //causes a tick event

if ($a > 0) {
  $a += 3;       //causes a tick event
  echo $a;       //causes a tick event
}
?>

The output of the above code will be:

tick_handler() is called
tick_handler() is called
tick_handler() is called
4tick_handler() is called

Example: using args parameter

Consider the example below, where this function is used to pass an additional parameter to the tick handler function.

<?php
declare(ticks=1);

//a function called on each tick event
function tick_handler($user) {
  echo "Hello $user, tick_handler() is called\n";
}

$user = "John";

//registering a tick function
register_tick_function('tick_handler', $user); //causes a tick event

$a = 1;          //causes a tick event

if ($a > 0) {
  $a += 3;       //causes a tick event
  echo $a;       //causes a tick event
}
?>

The output of the above code will be:

Hello John, tick_handler() is called
Hello John, tick_handler() is called
Hello John, tick_handler() is called
4Hello John, tick_handler() is called

Example: using multiple tick functions

In the example below, multiple tick handler functions are used. Each function is be called in the same order as they were registered.

<?php
//functions called on each tick event
function tick_handler1() {
  echo "tick_handler1() is called\n";
}

function tick_handler2() {
  echo "tick_handler2() is called\n\n";
}

//registering tick functions
register_tick_function('tick_handler1');
register_tick_function('tick_handler2');

declare(ticks=1); //causes a tick event

$a = 1;           //causes a tick event
if ($a > 0) {
  $a += 3;        //causes a tick event
}
?>

The output of the above code will be similar to:

tick_handler1() is called
tick_handler2() is called

tick_handler1() is called
tick_handler2() is called

tick_handler1() is called
tick_handler2() is called

Example: using object method

A tick handler function is also be used as an object method. Consider the example given below:

<?php
declare(ticks=1);

class my_class {
  public function my_method() {
    echo "Hello World!\n";
  }
}

//using an object->method
$object = new my_class();

//registering the object method as
//tick function - also causes a tick event
register_tick_function(array($object, 'my_method'), true);

$a = 1;           //causes a tick event
if ($a > 0) {
  $a += 3;        //causes a tick event
} 
?>

The output of the above code will be similar to:

Hello World!
Hello World!
Hello World!

❮ PHP Function Handling Reference

5