The declare structure is used to set the execution command of a piece of code. Its syntax structure is as follows: declare (directive) & amp; nbsp; statement doesn't understand? The general explanation is as follows: Decia
The declare structure is used to set the execution command of a piece of code. Its syntax structure is as follows:
Declare (directive)
Statement
Don't you understand? The general explanation is as follows: declare is the PHP process control structure. directive currently supports two commands [ticks and encoding]. the use of ticks must be used with the register_tick_function function (of course, the unregister_tick_function function. The ticks parameter indicates how many statements are run to call the register_tick_function function once.
The register_tick_function function defines the processing function for each tick event. So what is a tick event?
- Tick 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 operations that should be performed when a tick event occurs.
The problem arises again. what is a low-level statement? It includes:
- Simple statement: empty statement (just one; number), return, break, continue, throw, goto, global, static, unset, echo, built-in HTML text, A semicolon-terminated expression is considered as a statement.
- Composite statement: The Complete if/elseif, while, do... while, for, foreach, switch, try... catch and so on are regarded as a statement.
- Statement block.
- The last note is that the declare block itself is also a statement (in principle, the declare block is also a composite statement, but it is specially isolated here ).
Let's look at a simple example:
/** * declare * @link http://www.phpddt.com */function do_tick(){ echo "do_tick";}register_tick_function('do_tick');declare(ticks = 1){ for($i = 1; $i < 5; $i++) { echo "{$i}
"; }}
The running result of the above program:
Explain the result as follows:
- Enter the for loop for the first time, execute echo and then execute the do_tick function for the first time;
- After the first for loop ends, execute the second do_tick function;
- Loop four times ......;
- The fifth for loop is performed. if the condition is not met, the for loop ends and the do_tick function is executed;
- Declare itself is also a statement and also needs to execute the do_tick function once.
As shown in the preceding figure, declare and ticks can be used for debugging or performance testing.