18

I'd like to know why, how and when to use ticks in PHP:

declare(ticks=1);

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

register_tick_function('tick_handler');

$a = 1;

if ($a > 0) {
    $a += 2;
    print($a);
}
strager
  • 88,763
  • 26
  • 134
  • 176
James Tang
  • 593
  • 6
  • 13
  • possible duplicate of [In PHP, what is a Tick?](http://stackoverflow.com/questions/1629005/in-php-what-is-a-tick) – Alix Axel Sep 07 '10 at 10:30

4 Answers4

15

One use was outlined by rosen_ivanov@abv.bg:

As Chris already noted, ticks doesn't make your script multi-threaded, but they are still great. I use them mainly for profiling - for example, placing the following at the very beginning of the script allows you to monitor its memory usage:

<?php

function profiler($return=false) {
    static $m=0;
    if ($return) return "$m bytes";
    if (($mem=memory_get_usage())>$m) $m = $mem;
}

register_tick_function('profiler');
declare(ticks=1);

/*
Your code here
*/

echo profiler(true);

?>

This approach is more accurate than calling memory_get_usage only in the end of the script. It has some performance overhead though :)

Another use was described by warhog@warhog.net:

as i read about ticks the first time i thought "wtf, useless crap" - but then i discovered some usefull application...

you can declare a tick-function which checks each n executions of your script whether the connection is still alive or not, very usefull for some kind of scripts to decrease serverload

<?php

function check_connection()
{ if (connection_aborted())
   { // do something here, e.g. close database connections
      // (or  use a shutdown function for this
      exit; }
}

register_tick_function("connection");

declare (ticks=20)
{
  // put your PHP-Script here
  // you may increase/decrease the number of ticks
}

?>
strager
  • 88,763
  • 26
  • 134
  • 176
7

Ticks can be used for basic things like:

  1. Profiling your scripts
  2. Monitor memory usage
  3. Count execution time
  4. Check resources, e.g. that a database connection is live

In PHP 4 you could use ticks to implement exception-like error handling.

Ticks can be used for other things too, like implementing an event driven application (e.g. a game).

jmz
  • 5,399
  • 27
  • 29
3

A tick is an event that occurs for every N low-level statements executed by the parser within the declare block. The value for N is specified using ticks=N within the declare blocks's directive section.

The event(s) that occur on each tick are specified using the register_tick_function().

shamittomar
  • 46,210
  • 12
  • 74
  • 78
  • 2
    I think @James Tang is asking for what this feature is used for. I think he understands what ticks are and what they do. – strager Sep 07 '10 at 06:42
  • 1
    @stranger, agree but the `how` part of the question triggered me to post this answer. – shamittomar Sep 07 '10 at 06:45
  • 4
    as it is (ctrl+c) (ctrl+v) from [php.net](http://www.php.net/manual/en/control-structures.declare.php#control-structures.declare.ticks) – Jatin Dhoot Apr 14 '11 at 08:34
0

I found a particularly interesting use case for ticks not outlined here yet.

I was facing a scenario in my daemon where i wanted to use pcntl_fork and ensure that it happened precisely when i expected too but the symptoms where showing me otherwise. The problem boils down into 2 parts, (1) How zend-ng (PHP7's new engine) collates C executions based on your compiled PHP directives, i.e. between each group of executions we have a 'tick', and (2) How resources are exposed to you in PHP, i.e. file descriptors are created on the O/S by the C code which may or may not be in the expected execution block when compiled from your code into C.

In simple terms, I opened a socket in the parent process and in a child process used it, simple right? well no, the resource in the child process wasn't always there as was expected and in all cases the parent process was not terminated (which normally explains why you lose access to open resources)

Forcing PHP to announce when a tick is done after one execution block actually forced zend-ng to be a little less efficient and ensure my blocks of execution weren running as expected.

Stof
  • 610
  • 7
  • 16