在php中循环时显示输出

Nei*_*val 4 php

在无限循环中是否可以在浏览器上显示字符串?这就是我想要发生的事情:

while(1) {
     echo "should display these lines on browser while in infinite loop.<br>";
}
Run Code Online (Sandbox Code Playgroud)

mea*_*gar 13

对的,这是可能的.如果希望立即显示输出,则需要将输出刷新到浏览器:

while(1) {
     echo "should display these lines on browser while in infinite loop.<br>";
     flush();
}
Run Code Online (Sandbox Code Playgroud)

有可能无论你想要完成什么,这都不是你应该怎么做的.

PHP最终会超时,但在它生成大量 HTML文档之前,浏览器将无法显示.


Ben*_*ier 5

注意使用ob_flush();来确保 php 输出,并usleep(100000)有时间查看正在发生的事情。

while(1) {
     echo "should display these lines on browser while in infinite loop.<br>";
     usleep(100000); // debuging purpose
     ob_flush();
     flush();
}
Run Code Online (Sandbox Code Playgroud)