Here's something odd, I though this would output a page and show part by part, until all is loaded (similar to how Wordpress update/reinstall process works):
这里有一些奇怪的事情,我认为这将输出一个页面并部分地显示,直到全部被加载(类似于Wordpress更新/重新安装过程的工作方式):
<html>
<body>
<?php
for( $i=0; $i<100; $i++)
{
echo 'HELLO';
}
sleep(10);
echo '<p></p>';
for( $i=0; $i<100; $i++)
{
echo 'THERE';
}
sleep(10);
echo '<p></p>';
for( $i=0; $i<100; $i++)
{
echo 'HOW ';
}
sleep(10);
echo '<p></p>';
for( $i=0; $i<100; $i++)
{
echo 'ARE U';
}
sleep(10);
echo '<p></p>';
Oddly enough it waits for the entire page, then shows it. What variables/configuration affect this behavior?
奇怪的是,它等待整个页面,然后显示它。哪些变量/配置会影响这种行为?
3 个解决方案
#1
6
I dont know how you think php works, but it is only server side and you are expecting partial updates in client side. PHP does not work like that.
我不知道您认为php如何工作,但它只是服务器端,您希望在客户端进行部分更新。PHP不是这样工作的。
When you call that php script, php interpreter begin to execute it, putting the output into temporary location, then with the sleep function, php interpreter waits until milliseconds are reached, then continue executing, concatenating the output in the temp location, and go on... after all script execution is done, php sends the whole output to the client.
当您调用php脚本时,php解释器开始执行它,将输出放入临时位置,然后使用sleep函数,php解释器等待毫秒数到达,然后继续执行,将输出连接到临时位置,然后继续……完成所有脚本执行之后,php将整个输出发送给客户端。
If you need partial updates on a page, you need to enter in the world of async calls and ajax.
如果需要对页面进行部分更新,则需要进入异步调用和ajax的世界。
#2
5
It buffers the output and only send it when the page is finished loading or, afaik, after a few kilobytes were buffered. You can control this functionality by either completely wrapping your code in ob_start()
and ob_end_flush()
, or in this case, calling flush()
before every sleep.
它缓冲输出,只在页面完成加载时发送,或者在缓冲了几千个字节后发送。您可以通过在ob_start()和ob_end_flush()中完全包装代码来控制这个功能,或者在本例中,在每次睡眠之前调用flush()。
More info: What is output buffering?
更多信息:输出缓冲是什么?
#3
1
php is server side. Have you tried this with javascript's setTimeout()
function?
php服务器端。您是否尝试过javascript的setTimeout()函数?
Here's an example of a string that's repeated 10 times after a 5 second delay.
这是一个字符串在5秒延迟后重复10次的例子。
<script>
setTimeout(test, 5000);
function test(){
for (var i=0;i<10;i++){
document.write("test <br />");
}
}
</script>
#1
6
I dont know how you think php works, but it is only server side and you are expecting partial updates in client side. PHP does not work like that.
我不知道您认为php如何工作,但它只是服务器端,您希望在客户端进行部分更新。PHP不是这样工作的。
When you call that php script, php interpreter begin to execute it, putting the output into temporary location, then with the sleep function, php interpreter waits until milliseconds are reached, then continue executing, concatenating the output in the temp location, and go on... after all script execution is done, php sends the whole output to the client.
当您调用php脚本时,php解释器开始执行它,将输出放入临时位置,然后使用sleep函数,php解释器等待毫秒数到达,然后继续执行,将输出连接到临时位置,然后继续……完成所有脚本执行之后,php将整个输出发送给客户端。
If you need partial updates on a page, you need to enter in the world of async calls and ajax.
如果需要对页面进行部分更新,则需要进入异步调用和ajax的世界。
#2
5
It buffers the output and only send it when the page is finished loading or, afaik, after a few kilobytes were buffered. You can control this functionality by either completely wrapping your code in ob_start()
and ob_end_flush()
, or in this case, calling flush()
before every sleep.
它缓冲输出,只在页面完成加载时发送,或者在缓冲了几千个字节后发送。您可以通过在ob_start()和ob_end_flush()中完全包装代码来控制这个功能,或者在本例中,在每次睡眠之前调用flush()。
More info: What is output buffering?
更多信息:输出缓冲是什么?
#3
1
php is server side. Have you tried this with javascript's setTimeout()
function?
php服务器端。您是否尝试过javascript的setTimeout()函数?
Here's an example of a string that's repeated 10 times after a 5 second delay.
这是一个字符串在5秒延迟后重复10次的例子。
<script>
setTimeout(test, 5000);
function test(){
for (var i=0;i<10;i++){
document.write("test <br />");
}
}
</script>