为什么当php忙时echo不显示任何内容

EBA*_*BAG 3 php

当PHP忙于某些事情时(例如用curl或类似的东西上网时),PHP Echo或Print函数不会显示任何内容.

后来我发现php在命令行执行你的php时会显示输出:

php myscript.php
Run Code Online (Sandbox Code Playgroud)

但是现在我也没有从命令行获得任何输出!有什么样的技巧或设置应该让PHP显示输出?

Tar*_*rka 6

机会是,它正在缓存结果(在PHP和Web服务器中),而不是实际将它们发送到浏览器.我能给出的最好的建议是我的代码中的这个块:

/**
 * Act as a "breakpoint" in the code, forcing everything to be flushed to the browser
 */
function breakpoint() {
    global $debug;
    if ($debug) { // So that it doesn't slow down extracts
        ob_flush();
        flush();
        sleep(.1);
    }
}
Run Code Online (Sandbox Code Playgroud)

问题$debug是,如果我们在调试模式下运行页面,特定于我们的网站.

您需要的两件事是ob_flush(),它将PHP的缓冲区发送到Web服务器的缓冲区,flush()这将导致服务器转储到浏览器.(注意:如果浏览器在显示任何内容之前缓存,则没有什么可以防止这种情况)这sleep有助于确保它不会过载,并且有机会正确刷新.

请参阅:http: //ca.php.net/manual/en/function.ob-flush.phphttp://ca.php.net/manual/en/function.flush.php