WAMP and flush()


WAMP and flush()

我正在尝试通过让字符串迭代一定次数来在我的 WAMP 服务器上实现一个简单的 while 循环。 但是,尽管在 WAMP PHP 设置中关闭了输出缓冲,但整个输出会立即发生。

版本 1

$i = 0;
while ($i < 5)
{
print ("This is an example of a while loop.<br/>");
flush();
sleep(1);
$i++;
}

版本 2

$i = 0;
while ($i < 5)
{
print ("This is an example of a while loop.<br/>");
ob_start();
ob_flush();
flush();
sleep(1);
$i++;
}

这两个版本都没有按照我想要的方式输出字符串,即以一秒的间隔一次输出一个字符串。 任何帮助将不胜感激。

我一直在 WAMP 和同花顺方面遇到问题,最终得出的结论是它在 WAMP 中被破坏了。似乎无论我拥有什么服务器设置,WAMP 的打包方式都有些不起作用。

要让它工作的唯一方法是使用 XAMPP,或者安装和配置您自己的服务器。

我遇到了这个问题,并且能够通过合并以下代码来解决它。

// Necessary Settings and stuff for output buffering to work right
apache_setenv('no-gzip', 1);
ini_set('zlib.output_compression', 0);
ini_set('implicit_flush', 1);
ini_set('output_buffering', "off");
// Start a new output buffer and send some blank data to trick browsers
ob_start();
echo str_repeat(" ", 4096);
for ($i=0; $i < 10; $i++) {
    echo "<div>Echo Something...</div>'n";
    // Call both ob_flush and flush functions
    ob_flush();
    flush();
}

对于那些即使使用贾斯汀的解决方法也无法做到的人。在 wamp x64 上测试。

// Necessary Settings and stuff for output buffering to work right
apache_setenv('no-gzip', 1);
ini_set('zlib.output_compression', 0);
ini_set('implicit_flush', 1);
ini_set('output_buffering', "off");
// Start a new output buffer and send some blank data to trick browsers
ob_start();
echo str_repeat(" ", 4096);
ob_end_flush(); //addition
ob_flush();
flush();
for ($i=0; $i < 10; $i++) {
    ob_start(); //addition
    echo "<div>Echo Something...</div>'n";
    ob_end_flush(); //addition
    ob_flush();
    flush();
}