Home >Backend Development >PHP Tutorial >PHP declare control characters and ticks tutorial (with examples)_PHP tutorial
The
declare structure is used to set the execution instructions of a piece of code. Its syntax structure is as follows:
Don’t understand? The popular explanation is as follows: declare is the process control structure of PHP. Directive currently supports two instructions [ticks and encoding]. The use of ticks needs to be used in conjunction with the register_tick_function function (and of course the unregister_tick_function function). The ticks parameter indicates how many statements to run to call the register_tick_function function once.
The 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 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 operation that should be performed when the tick event occurs.
The question comes again, what is a low-level statement? It includes:
Simple statements: empty statement (just a ; sign), return, break, continue, throw, goto, global, static, unset, echo, built-in HTML text, expressions terminated by semicolon, etc. are all counted a statement.
Compound statement: complete if/elseif, while, do...while, for, foreach, switch, try...catch, etc. are counted as one statement.
Statement block: statement block enclosed by {}.
The last special thing: the declare block itself is also a statement (it stands to reason that the declare block is also a compound statement, but it is deliberately separated here).
Look at a simple example:
declare(ticks = 1)
{
for($i = 1; $i < 5; $i++)
{
echo "{$ i}
";
}
}