PHP Declare Stataement
What is declare() statements?
Explanation
The declare statement is used to set execution directive for a block of codes.
Syntax:
declare (directive)
{statements}
There are two directives in PHP they are "ticks", "Encoding". The "statements" will be executed based on the directive set. The declare statements can be used with global scope affecting all the code following it.
Example :
<?php
function myfunc() {
print "In tick funcn";
}
register_tick_function("myfunc");
declare(ticks=10) {
for($i = 0; $i < 20; ++$i) {
print "Hellon";
}
}
declare(ticks=4) {
for($i = 0; $i < 20; ++$i) {
print "Hellon";
}
}
?>
In the above example 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.