简体   繁体   English

运行后的PHP Cli输出

[英]PHP Cli Output after run

for($i = 0; $i < 5; $i++) {
      echo $i . "\n";
      sleep(1);
}

This is the code I run. 这是我运行的代码。 Instead of showing a number each second, the php CLI decides to wait and after everything is executed shows 而不是每秒显示一个数字,PHP CLI决定等待并在执行所有操作后显示

0
1
2
3
4

Why is this happening, how can I make it "real-time" ? 为什么会发生这种情况,我如何使其“实时”?

EDIT : Found the problem: because I included WP-Core (Wordpress) the output somehow buffered, if I remove the wp-core it is all fine. 编辑 :发现了问题:因为我以某种方式缓冲了WP-Core(Wordpress)的输出,如果我删除了wp-core,一切都很好。 For more info, when including the wp_core there are some wp notices that are being logged in separate file. 有关更多信息,当包含wp_core时,有一些wp通知正在单独的文件中记录。

You need to disable output buffering, or flush the buffer. 您需要禁用输出缓冲或刷新缓冲区。 You really only need the ob for requests that come over HTTP, as it lets you do all your server-side processing without having to worry about the connection to the client. 您实际上只需要ob来处理通过HTTP发出的请求,因为它使您可以进行所有服务器端处理,而不必担心与客户端的连接。

for($i = 0; $i < 5; $i++) {
      echo $i . "\n";
      sleep(1);

      flush();
      ob_flush();
}

Alternatively, if you can't change the code, you can disable output buffering completely in your CLI php.ini 另外,如果您无法更改代码,则可以在CLI php.ini中完全禁用输出缓冲

output_buffering=Off

Servers usually buffer the output of a server side script until there's enough in it to output try something like this. Servers通常会缓冲服务器端脚本的输出,直到其中有足够的输出来尝试这样的操作。 Combination of setting output buffering off and manually flushing the buffer. 关闭输出缓冲设置和手动刷新缓冲区的组合。 Note the implcit flush line and the flush and ob_flush lines. 注意隐式flush行以及flush和ob_flush行。

for ($i = 0; $i < 5; $i++) {
    echo $i . "</br>";
    sleep(1);
    flush();
    ob_flush();
}

for more info about ob-flush please read http://php.net/manual/en/function.ob-flush.php 有关ob-flush更多信息,请阅读http://php.net/manual/zh/function.ob-flush.php

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM