Today I want to talk about a construct many have never used, nor heard of: declare. This construct, when used in conjunction with ticks and register_tick_function, is an extremely powerful and useful tool. If one were to read the definition and usage on the manual, it’s usage is not immediately clear. But don’t worry, it even took me about 2 hours to finally see just what exactly this would be useful for.

With that in mind, today’s article will be on ticks, what they are, how to use them, and how they will benefit you. Keep in mind that this is a more advanced tutorial and some may have trouble understanding all these usages (or even the importance), but I’ll try my best to explain so everyone has at least a more solid understanding of this great construct.

What is a Tick?

Despite the introductory image, ticks in PHP are not evil insects looking to suck the life out of you. But before we can understand what a tick is, we must understand what a declare construct is, which our “ticks” are created with. The PHP manual defines it as follows:

The declare construct is used to set execution directives for a block of code. The syntax of declare is similar to the syntax of other flow control constructs:

declare (directive)
    statement

The directive section allows the behavior of the declare block to be set. Currently only two directives are recognized: the ticks directive (See below for more information on the ticks directive) and the encoding directive (See below for more information on the encoding directive).

Note: The encoding directive was added in PHP 5.3.0

The statement part of the declare block will be executed – how it is executed and what side effects occur during execution may depend on the directive set in the directive block.

The declare construct can also be used in the global scope, affecting all code following it (however if the file with declare was included then it does not affect the parent file).

If you are scratching your head trying to understand this, you are not alone. While the above makes sense once you realize what declare really is, but being able to actually undestand it can be challenging. To explain this in a bit more detail, let’s take a look at an example of code other than what the PHP manual wants to show us.

Note: This example is only 1 of 2 methods to use the “declare” construct. I will explain the second later in this article. For now, we’ll use the easiest to understand.

1
2
3
4
5
6
7
8
9
10
<?php
function my_callback_function() {
  echo "I was called from a tick!<br />";
}

declare(ticks = 1);
register_tick_function("my_callback_function");

$x = 10;
?>

The above example consists of 4 major parts: a function definition, a “declare” construct, a “register_tick_function” call, and a simple variable assignment. The first thing we’re going to talk about though is the “declare construct”.

In our example, we have the follwing bit of code:

1
declare(ticks = 1);

This sets the stage for how many lines of actual PHP code we are going to tick (or, for a better use of the word, count). The definition “ticks = 1″ tells PHP that for every single bit of PHP code that it can “tick” (aside from condition expressions and argument expressions), it will keep a record of each one.

But before we can make use of these ticks, we should define a function that will keep track and use each tick that we encounter (you are not required to create a call-back function, though it is suggested to do so since you’re using the declare construct for a reason..). We do this with the following piece of code:

1
register_tick_function("my_callback_function");

Already, our callback function will have been called, resulting in the line “I was called from a tick!”, which means 1 tick has been executed. This part is a bit confusing, but the point is that once we issue “declare”, PHP will start “ticking” the very next line on until we stop the declare from working or PHP ends, whichever comes first.

You will notice 2 more “I was called from a tick!” statements on your browser. The second echo statement should be obvious since PHP found our $x = 10; statement, resulting in the next tick. But where is this third tick coming from? If you guessed the close tag of PHP (?>) you would be correct! PHP will report ticks all the way down to the very last PHP code (in this case, the closing ?> tag)!

In the note above, it is indicated that we can use ticks 2 different ways (including the first example shown above). Here is the other example to get almost the exact same result:

1
2
3
4
5
6
7
8
9
10
11
<?php
function my_callback_function() {
  echo "I was called from a tick!<br />";
}

register_tick_function("my_callback_function");
declare(ticks = 1) {

$x = 10;
}
?>

If we execute this piece of code and observe the output to our browser, we’ll notice that instead of 3 “I was called from a tick!” messages, we’ll only see 2! Why? Because even though we “registered” our callback function for the declare construct, we hadn’t actually called the declare construct! In other words, PHP will not start “ticking” until we actually call the declare construct, but will even issue a tick for our closing brace in this declaration, just like it would issue one for our ending PHP tag.

A question would be, though, “Why is there not another “I was called from a tick!” to account for the opening curly brace?”. That’s because, in this last example, the opening curly brace is what actually tells PHP to start crunching code, not the definition alone by itself.

Before we get any further into this subject, I want to show you all something that will “almost” make everything you understand so far seem to fly right out the window. So why am I showing it to you? So that you understand how PHP operates. Note this example:

1
2
3
4
5
6
7
8
9
<?php
function my_callback_function() {
  echo "I was called from a tick!<br />";
}

register_tick_function("my_callback_function");
declare(ticks = 1) {
}
?>

Before we even execute this, one would assume that we would never see “I was called from a tick!”. That is not the case. You will actually see it 3 times! What is going on here? I asked the exact same question so I turned to lots of articles, bug reports, and PHP forums to find out this is, indeed, a bug with the declare construct.  But seeing as how this is a very small bug (and one that really won’t be discovered in your scripts unless you just blatantly leave the code blank as above), it shouldn’t be that big of a deal.

I hope this has been a useful post for you. The potential of ticks is huge, and the results offer a great insight into debugging and code management. While I do not condone using this method in production, it is definitely a method to consider when in a staging/testing environment.