31

I am a little bit confused by the PHP function declare.

What exactly is a single tick? I thought a tick equals one line of code?

But if I use:

function myfunc() {
        print "Tick";   
}

register_tick_function("myfunc");

declare(ticks=1) {
   echo 'foo!bar';
}

The script prints:

"Tick" 2 Times??

Unheilig
  • 16,196
  • 193
  • 68
  • 98
opHASnoNAME
  • 20,224
  • 26
  • 98
  • 143

2 Answers2

24

You get a tick for each line ; and each block {} Try that:

declare(ticks=1) echo 'foo!bar';

No block, no extra tick.

declare(ticks=1) {{ echo 'foo!bar'; }}

More extraneous blocks = more ticks.

PS: by the way, ticks are quite the exotic feature and they're only useful in a few extremely rare situations. They are not equivalent to threading or anything. If, for you, ticks are the solution to a problem then you should post about your problem in another question because it's probably not the right solution to it.

Josh Davis
  • 28,400
  • 5
  • 52
  • 67
17

You are on the right track as to what a tick is.

http://www.tuxradar.com/practicalphp/4/21/0

Put simply, a tick is a special event that occurs internally in PHP each time it has executed a certain number of statements. These statements are internal to PHP and loosely correspond to the statements in your script. You can control how many statements it takes to set off a tick using the declare() function, and you can register functions to execute when a tick occurs by using the register_tick_function() function. As mentioned already, the syntax for declare is very unusual, so be ready for a shock!

bwoebi
  • 23,637
  • 5
  • 58
  • 79
Jayrox
  • 4,335
  • 4
  • 40
  • 43