WAMP和flush()

时间:2012-04-27 03:12:49

标签: php while-loop wamp

我试图通过让字符串迭代一定次数来在我的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++;

}

两个版本都没有按照我想要的方式输出字符串,即每隔一秒输出一个字符串。非常感谢任何帮助。

3 个答案:

答案 0 :(得分:0)

我总是遇到WAMP和同花顺的问题,最终得出的结论是它在WAMP中被打破了。似乎无论我有什么样的服务器设置,都有关于WAMP打包方式的一些内容,这些内容无法正常工作。

使用XAMPP,或者安装和配置自己的服务器,唯一可行的方法就是使用它。

答案 1 :(得分:0)

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

// 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();
}

答案 2 :(得分:0)

对于那些即使用Justin的解决方法也无法做到的人。在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();
}