首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PHP:为什么声明(ticks=1)打印4次

PHP:为什么声明(ticks=1)打印4次
EN

Stack Overflow用户
提问于 2017-01-25 06:28:53
回答 1查看 74关注 0票数 1

我正在学习php声明语句,我播下了以下php代码:

代码语言:javascript
复制
<?php

declare(ticks=1);
// A function called on each tick event
function tick_handler()
{
    echo "<br>tick_handler() called ";
}

register_tick_function('tick_handler');

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

?>

产出:

代码语言:javascript
复制
tick_handler() called
tick_handler() called
tick_handler() called 3
tick_handler() called 

我不明白为什么"tick_handler() called"要打印4次,为什么" tick_handler() called 3"出现在第三次print.please帮助我使用简单的explanation.thanks

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-01-25 06:47:26

实际上,您很困惑,因为您的代码structure.It需要链接如下:-

代码语言:javascript
复制
<?php

declare(ticks=1); //this is a tick so tick_handler() will call and first time it outputs
// A function called on each tick event
function tick_handler()
{
    echo "tick_handler() called ";
    echo PHP_EOL;
}

register_tick_function('tick_handler');

$a = 1; // this a tick so again tick_handler() will call and second times output
if ($a > 0) {
    $a += 2; // this is a tick so again tick_handler() will call and third times output
    print($a);//  it prints first the $a  and because its a tick again so tick_handler() will call and fourth times output
}

?>

输出:-https://eval.in/723678

注:-在链接的第二个示例中可以找到更清晰的图片:- http://php.net/manual/en/control-structures.declare.php#control-structures.declare.ticks

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41844858

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档