php里的declare用法

时间:2021-05-20 09:01:59

function tick_handler ()
{
echo "tick_handler() called<br>" ;
}
function tick_handler1 ()
{
echo "tick_handler1() called<br>" ;
}


register_tick_function ( 'tick_handler' );
register_tick_function ( 'tick_handler1' );

declare( ticks = 1 ){
$a = 1 ;

if ( $a > 0 ) {
$a += 2 ;
print( $a );
}
}
exit;

这里首先定义了两个函数,然后注册到系统,随后通过declare定义一个ticks,步长为1,作用是在declare作用域中的代码每执行ticks次步长,就执行一次注册的方法,方法顺与注册的顺序一致。

所以上面的执行结果是:

tick_handler() called
tick_handler1() called
tick_handler() called
tick_handler1() called
3tick_handler() called
tick_handler1() called
tick_handler() called
tick_handler1() called
tick_handler() called
tick_handler1() called
tick_handler() called
tick_handler1() called

这个方法可以测试执行代码的处理时间。