void register_tick_function ( callback function [, mixed arguments])
void unregister_tick_function ( string function_name)
Ticks have been deprecated as of PHP 5.3, and will be removed entirely at some point in the future. Don't worry, though - very few people even knew what they were, and even fewer used them. Actually, we expect no one used them! Still, we've left the documentation here as historical reference. Instead, define() is now used to declare the scripts encoding, eg "declare(encoding='ISO-8859-1');" or "declare(encoding='UTF-8');". You must no longer use it with braces as was previously optional.
There is one last set of function calls you need to be aware of, and the first of them is not really a function: declare(). I say "it is not really a function" because it is technically a language construct, and therefore behaves unusually. In fact, the usage for declare() was so wholly unlike anything else in PHP that you will not have to use it very often - the most common use is to handle process control.
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!
Here is an example of these two in action:
<?php
function myfunc() {
print "In tick func\n";manual/en/intro.wddx.php
}
register_tick_function("myfunc");
declare(ticks=10) {
for($i = 0; $i < 20; ++$i) {
print "Hello\n";
}
}
declare(ticks=4) {
for($i = 0; $i < 20; ++$i) {
print "Hello\n";
}
}
?>
Firstly, note that the function myfunc() is used as the tick function, which means myfunc() will be executed each time a tick occurs. Now, notice the first use of declare() - "ticks" is set to 10 and a code block is opened, which means that until the matching brace is reached and the block is closed, ticks will occur every ten statements. Inside the declare() statement is a loop that prints out "Hello" twenty times, so you might think myfunc() will be called just twice because twenty divided by ten is two. However, myfunc() will in fact be called four times because the loop iterator is an internal statement also, which brings the total number of statements up to 40.
Immediately after the first declare() block there is a second declare() block where ticks is set to 4 rather than 10, which in turn makes the tick functions execute more often. Although it's fairly rare that you would use two different tick values in one script, it is important to at least know that it can be done.
The declare() "function" can also be used on a global scope, which means you set it once and can forget about it. This is a better choice if you do not plan on having multiple ticks values, as you can just set it and forget it.
To set ticks globally, use declare() like this:
declare(ticks=100);
You can call it multiple times in one script, however if you plan to do so you will likely find it better to use the block notation as it makes the tick setting more obvious. You can also register as many tick functions as you like, or unregister them using unregister_tick_function(). You can even pass in various parameters that you would like sent to the tick functions. All three of these techniques are shown in this next code block:
<?php
function myfunc($param1, $param2) {
echo "In first tick function with params $param1 $param2\n";
}
function myfunc2($param1, $param2, $param3) {
echo "In second tick function with params $param1 $param2 $param3\n";
}
function myfunc3($param1) {
echo "In third tick function with params $param1\n";
}
register_tick_function("myfunc", "hello", "world");
register_tick_function("myfunc2", "how", "are", "you?");
register_tick_function("myfunc3", "goodbye!");
unregister_tick_function("myfunc2");
declare(ticks=10);
for($i = 0; $i < 20; ++$i) {
echo "Hello\n";
}
?>
Author's Note: Many people might think of ticks as being a quick way of achieving multithreading, which is not really ideal. Firstly, the script still executes in parallel: when a tick occurs, PHP stops what it was doing, executes all the tick functions, then goes back to where it left off. Under no situation does PHP execute both the tick function and the original code simultaneously. If you want to have your PHP script do more than one thing at once, flick on ahead to learn about process control.
Also note that, in my opinion, ticks are a dying breed (edit: wow, this was eerily prescient...). They were really only implemented as a hack to begin with, and few people even know how they work never mind use them in their own script. Although I doubt ticks will ever be deprecated (edit: OK, so that bit I got wrong!), you should be under no illusions that hiring PHP developers with ticks experiences is very hard!
Want to learn PHP 7?
Hacking with PHP has been fully updated for PHP 7, and is now available as a downloadable PDF. Get over 1200 pages of hands-on PHP learning today!
If this was helpful, please take a moment to tell others about Hacking with PHP by tweeting about it!
Next chapter: Handling non-English characters >>
Previous chapter: Callback functions
Jump to:
Home: Table of Contents
Copyright ©2015 Paul Hudson. Follow me: @twostraws.