Home >Backend Development >PHP Tutorial >PHP: Detailed explanation of usage of declare

PHP: Detailed explanation of usage of declare

黄舟
黄舟Original
2018-05-14 16:04:1912938browse

declare The structure is used to set the execution instructions of a piece of code. The syntax of declare is similar to other Process Control structures:

declare (directive)
The statement

directive part allows setting the behavior of the declare code segment. Currently only two commands are recognized: ticks (see the ticks command below for more information) and encoding (see the encoding command below for more information).

Note: encoding is a new directive in PHP 5.3.0.

declare The statement part of the code segment will be executed - how it is executed and what side effects occur during execution depend on the instructions set in the directive.

The declare structure can also be used in the global scope, affecting all subsequent code (but if a file with a declare structure is included by other files, it will not affect the parent file containing it).

<? 
    declare (ticks = 1); //这句这么写表示全局的脚本都做处理
    function foo() { //注册的函数
        static $no;
        $no++;
        echo $no."======";
        echo microtime()."\n";
    } 
    register_tick_function("foo"); //注册函数,后面可以跟第2个参数,表示函数的参数
    $a = 1;
    for($i=0;$i<5;$i++) { //这里的循环也是语句,会做一次判断$i<5的判断执行
        $b = 1;
    }
    ?>

declare is used to debug internal programs.
Let’s briefly explain that the declare function only supports one parameter, which is ticks. The function represents the recording program block and needs to be used with the register_tick_function function. The ticks parameter indicates how many statements to run to call the register_tick_function function once. And declare supports two writing methods:
1. declare(ticks = 1); the entire script
2. declare(ticks = 1) { Internal code records

}
Above In addition to the function body, the code will be executed outside. You can see the execution times and times when running. It is suitable for the execution time and execution times of each step in the test code segment.
declare must be global and placed outside the program.
tick represents an event, the event is defined in register_tick_function; the execution frequency of the event is (ticks=3).
Indicates that the event frequency is recorded once after 3 executions. The printing time of microtime().

The above is the detailed content of PHP: Detailed explanation of usage of declare. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn