Page 1 of 1
help need on ob_start()
Posted: Thu Aug 13, 2009 9:57 am
by codingislife
Hi
I am a newbie in php, I have a code
Code: Select all
<?php
ob_start();
for($i=1;$i<=10;$i++)
{
sleep(1);
echo $i;
ob_flush();
}
?>
I am trying to display one number in each second, but the browser showing all the numbers at once. where is the error ? how can I modify this code to show such a way
Thanks

Re: help need on ob_start()
Posted: Thu Aug 13, 2009 10:02 am
by neuroxik
You flush the buffer inside the first loop, that's why.
I'm not sure if it would work, but in any case, you should close the buffer AFTER your looping, like this:
Code: Select all
ob_start();
for($i=1;$i<=10;$i++)
{
sleep(1);
echo $i;
}
ob_flush();
UPDATE: JUst tried it and doesn't work, just as thought. Buffers normally allow you to do stuff that normally can't be done before headers have started to be sent or similar, but you'd have to (from my knowledge) use front-end coding, example javascript, to accomplish that.
Re: help need on ob_start()
Posted: Thu Aug 13, 2009 10:04 am
by SidewinderX
Can this even be done? Won't it just "display" one number per second on the server - then send it to the browser all at once?
Re: help need on ob_start()
Posted: Thu Aug 13, 2009 10:05 am
by neuroxik
SidewinderX wrote:Can this even be done? Won't it just "display" one number per second on the server - then send it to the browser all at once?
That's what I was thinking, I was just telling him that it would already be more reasonable to flush outside the loop if he tried.
Re: help need on ob_start()
Posted: Thu Aug 13, 2009 10:19 am
by codingislife
hi thanks for your reply
but I want to display 1,2,3,5.... in browser in each seconds. now all the contents are flushing out at the 10th second. I need to 1 in first second , 2 in the 2nd second , 3 in the 3rd second etc...
now it shows 12345... at the 10th second, the code that I am using so far is
Code: Select all
<?php
ob_start();
for($i=1;$i<=10;$i++)
{
sleep(1);
echo $i;
ob_flush();
}
?>
Re: help need on ob_start()
Posted: Thu Aug 13, 2009 10:21 am
by codingislife
hi thanks for your reply
but I want to display 1,2,3,5.... in browser in each seconds. now all the contents are flushing out at the 10th second. I need to 1 in first second , 2 in the 2nd second , 3 in the 3rd second etc...
now it shows 12345... at the 10th second, the code that I am using so far is
Code: Select all
<?php
ob_start();
for($i=1;$i<=10;$i++)
{
sleep(1);
echo $i;
ob_flush();
}
?>
Re: help need on ob_start()
Posted: Thu Aug 13, 2009 10:32 am
by neuroxik
Did you even read what we wrote? You even quoted your own uncorrected code.
Re: help need on ob_start()
Posted: Thu Aug 13, 2009 10:37 am
by codingislife
neuroxik wrote:Did you even read what we wrote? You even quoted your own uncorrected code.
I am talking about an example showing at
http://in3.php.net/manual/en/function.ob-start.php (the 2nd one ) edited by Hudson on 14-May-2009 09:16 , please check and reply me thanks
Re: help need on ob_start()
Posted: Fri Aug 14, 2009 4:22 am
by corecoder
please use the following code to do so
<?php
ob_end_flush();
$totalloops = 10;
for($i=1;$i<=$totalloops;$i++)
{
sleep(1);
echo $i;
flush();
}
?>
Re: help need on ob_start()
Posted: Fri Aug 14, 2009 4:23 am
by corecoder
neuroxik wrote:SidewinderX wrote:Can this even be done? Won't it just "display" one number per second on the server - then send it to the browser all at once?
That's what I was thinking, I was just telling him that it would already be more reasonable to flush outside the loop if he tried.
hey buddy, he can do so by the following code
please use the following code to do so
<?php
ob_end_flush();
$totalloops = 10;
for($i=1;$i<=$totalloops;$i++)
{
sleep(1);
echo $i;
flush();
}
?>
Re: help need on ob_start()
Posted: Fri Aug 14, 2009 8:00 am
by neuroxik
corecoder wrote:
<?php
ob_end_flush();
$totalloops = 10;
for($i=1;$i<=$totalloops;$i++)
{
sleep(1);
echo $i;
flush();
}
?>
Wow, this works.. never thought it would. Thanks gfor sharing

Re: help need on ob_start()
Posted: Thu Oct 22, 2009 1:00 pm
by wwuster
I'm trying to do the same thing as the original poster. But running the last code posted doesn't display one item per second in the browser. After 3 seconds it displays everything at once. So flush() is not working. This is using apache2, firefox, Ubuntu linux. Any idea why this doesn't work? I've tried MANY combinations of ob_flush(), flush(), setting compression options etc.
thanks,
William
$n = 3;
ob_end_flush();
for($i=0; $i<$n; $i++) {
sleep(1);
$str = sprintf("%d / %d", $i+1, $n);
echo $str;
flush();
}
$str = 'DONE!';
echo $str;