php的declare控制符和ticks教程(附示例)_PHP

WBOY
Release: 2016-06-01 11:55:40
Original
990 people have browsed it

declare 结构用来设定一段代码的执行指令,它的语法结构如下:
复制代码 代码如下:
declare (directive)
statement

不懂?通俗解释如下:declare是PHP的流程控制结构,directive目前支持两个指令【ticks和encoding】,ticks的使用需配合register_tick_function函数(当然还有unregister_tick_function函数)使用。ticks参数表示运行多少语句调用一次register_tick_function的函数。
register_tick_function函数定义了每个tick事件发生时的处理函数。那么什么是tick事件呢?
ick是一个事件。
tick事件在PHP每执行N条低级语句就发生一次,N由declare语句指定。
可以用register_tick_function()来指定tick事件发生时应该执行的操作。

问题又来了,什么是低级语句呢?它包括:
简单语句:空语句(就一个;号),return, break, continue, throw, goto, global, static, unset, echo,  内置的HTML文本,分号结束的表达式等均算一个语句。
复合语句:完整的if/elseif, while, do...while, for, foreach, switch, try...catch等算一个语句。
语句块:{} 括出来的语句块。
最后特别的:declare块本身也算一个语句(按道理declare块也算是复合语句,但此处特意将其独立出来)。

看一个简单的例子:

复制代码 代码如下:
function do_tick()
{
echo "do_tick";
}
register_tick_function('do_tick');

declare(ticks = 1)
{
        for($i = 1; $i         {
                echo "{$i}
";
        }
}

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!