The declare structure is used to set the execution instructions of a piece of code. Its syntax structure is as follows:
declare (directive)
statement
don't know? The common explanation is as follows: declare is the process control structure of PHP. Directive currently supports two instructions [ticks and encoding]. The use of ticks must be used with the register_tick_function function (of course, there is also the unregister_tick_function function). The ticks parameter indicates how many statements are run to call the register_tick_function function.
register_tick_function function defines the processing function when each tick event occurs. So what is a tick event?
Ick is an event.
The tick event occurs once every time N low-level statements are executed in PHP, and N is specified by the declare statement.
You can use register_tick_function() to specify the actions that should be performed when the tick event occurs.
The question is again, what are low-level statements? It includes:
Simple statements: empty statements (just one; number), return, break, continue, throw, goto, global, static, unset, echo, built-in HTML text, expressions ending with semicolons, etc. are all considered one statement.
Compound statement: the complete if/elseif, while, do...while, for, foreach, switch, try...catch, etc. are calculated as a statement.
Statement block: {} The statement block enclosed.
Finally, special: the declare block itself is also considered a statement (the declare block is also considered a compound statement, but it is specially independent here).
Let's take a look at a simple example:
function do_tick()
{
echo "<font color=red>do_tick</font>";
}
register_tick_function('do_tick');
declare(ticks = 1)
{
for($i = 1; $i < 5; $i++)
{
echo "<font color=blue>{$i}</font><br>";
}
}