PHP Using Declare? What Is a Tick

Understanding PHP declare() and ticks

When PHP is executing your script, the execution can be seen as a lot of statements being executed. Most statements cause a Tick, though not necessarily all statements do so. (Manual says: Typically, condition expressions and argument expressions are not tickable.)

This block would normally cause 5 ticks, as you are executing 5 statements:

$a = 1;
$B = 2;
$a = 3;
$B = 4;
$a = 5;

And this block would normally cause 5 ticks, and one more tick as the end of the while loop also is counted as a statement/tick:

while ($i < 5)
$a++;

With the help of declare(ticks=N) and register_tick_function(), you can now execute code in between the statements/ticks. The register_tick_function specifies which function should be called when a tick event occurs. And the declare sets how many tick should pass, before a tick event occurs.

With declare(ticks=1) and register_tick_function('someFunction'); you will call someFunction() code in between every statement/tick.

If you use declare(ticks=3), then someFunction() will be executed on every third statement/tick.

Example:

function handler(){
echo "x";
}
register_tick_function("handler");
$i = 0;
declare(ticks = 4) {
while ($i < 9)
echo ++$i;
}

This script will output: 1234x5678x9
It's that simple.

Now what is meant in the linked question with "whether the connection is still alive", is not really interesting on itself and is not actually related to the above mentioned. It is just something you COULD do on every tick event. But you can also do something totally different. What is mentioned is simply that some scripts can take quite some time to execute and that during the execution, the client can disconnect. (Imagine closing the browser, while the script is still running.) PHP will by default continue to run the script, even if the client has disconnected. You can use the function connection_aborted() to detect if the client has disconnected. This is something you COULD also do without using ticks at all.

Now let's say for example that you want your script to stop running as soon as the client disconnects. Simply use ...

function killme() {
if (connection_aborted()) {
die();
}
}
register_tick_function('killme');
declare(ticks=1);

... and your script will call killme() after each statement of your code. killme() will check if the client is still connected and die() when it isn't.

PHP using Declare ? What is a tick?

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.

In PHP, what is a Tick?

I found a decent explanation here. I have used them in writing daemons.

I think declare() might be planned for deprecation. I know it was at one point.

EDIT: It was the ticks directive that was planned for deprecation.

declare ticks and register tick function gives infinite loop

As the comments stated the code works, I assumed and replaced php.ini file and this works perfectly. Appreciate the quick help. Hope this thread helps someone.

how do php's declare(ticks) really work?

To use ticks in the global scope you have to have it in the beginning of the calling script. This is probably why you have to redeclare it. I cannot say for certain without knowing your code. Below are some examples that do work with unit testing.

You can IIRC declare youre ticks in your code by the following construct

function tick_function() {
// Do something
}

register_tick_function('tick_function');

declare (ticks=1) {
// Your code here
}

Or as a working example

function profile_memory()
{
echo '<!--' . memory_get_usage() . '-->';
}

register_tick_function('profile_memory');
declare (ticks=1)
{
$pass = md5('qwerty'); /* Tick executed */
$pass = strrev($pass); /* Tick executed */
echo $pass; /* Tick executed */
}

This is a working example of a self contained tick function that is working within a unit test

class TickTest {
private function profile_memory() {
static $i;
++$i;
echo "Tick $i\n";
}
public function __construct() {
}
public function doTicks() {
$register_tick_function = register_tick_function(
array($this,'profile_memory')
);
declare (ticks=1) {
$pass = md5('qwerty'); /* Tick executed */
$pass = strrev($pass); /* Tick executed */
}
}
}

And this is the unit test (and yes i'm aware of the fact that it's not a real test)

require_once 'PHPUnit/Framework.php';
require_once dirname(__FILE__).'/../ticks.php';
class TickTestTest extends PHPUnit_Framework_TestCase {
protected $object;
protected function setUp() {
$this->object = new TickTest;
}
protected function tearDown() {
}
public function testDoTicks() {
$this->object->doTicks();
}
}

Looking at the output the tick function is called when executing the unit test.

Some references

  • Tick functions
  • Ticks

PHP: why declare(ticks=1) printing 4 times

Actually your are confused because of your code structure.It need to be linke below:-

<?php

declare(ticks=1); //this is a tick so tick_handler() will call and first time it outputs
// A function called on each tick event
function tick_handler()
{
echo "tick_handler() called ";
echo PHP_EOL;
}

register_tick_function('tick_handler');

$a = 1; // this a tick so again tick_handler() will call and second times output
if ($a > 0) {
$a += 2; // this is a tick so again tick_handler() will call and third times output
print($a);// it prints first the $a and because its a tick again so tick_handler() will call and fourth times output
}

?>

output:-https://eval.in/723678

Note:- A more clear picture will be found in the second example of the link:- http://php.net/manual/en/control-structures.declare.php#control-structures.declare.ticks

What's the relation between declare(ticks) and a signal handler in php

Minor observation (quote the function name pls.):

pcntl_signal(SIGCHLD, 'sigHandler', false);

There are two different APIs involved.

  • The pcntl_wait() call is blocking until it gets an notification from the kernel.
  • The interrupt handling is an event loop inside the PHP interpreter. This is a hack feature and as of PHP5.3, there is a better way to do it ~ pcntl_signal_dispatch().
    • To answer to question, having declare ticks is like turning your mobile ringtone ON, or otherwise you never notice incoming calls.
    • The PHP5.3 method is much better engineering, and alot more controllable.
    • The default signal handler for most signals is sigignore, which receives the interrupt and does nothing. As you have registered the user handler I doubt this is being used.
    • I have never been able to discover the default value for the ticks, when not set directly.
    • Setting ticks to a small value does make scripts abit run slower, but you need to be doing heavy processing and monitoring to notice this. It makes a less predictable execution cost, I guess due to copying things around in the stack.
    • Without declare ticks or pcntl_signal_dispatch(), the signal is never picked up. If you are writing simple webpages, which terminate quickly; this may be the most sensible policy.
    • declare ticks needs to be tested carefully, as it has confusing scope rules. The safest method to put it at the head of your first script, like use strict in Perl.


Related Topics



Leave a reply



Submit