Progressively displaying the Output of the PHP Script

Discussion in 'PHP' started by netaddict, Aug 16, 2006.

  1. #1
    My Aim: I want to have a string (say "DP") to be keep displaying on the screen after every 5 seconds for 10 times.

    I am using the following code:

    
    <?php
    for($i=1;$i<=10;$i++)
    	{
    	sleep(5);
    	echo 'DP<br>';
    	}
    ?>
    
    PHP:
    But, what the above code does is - the script waits for 50 seconds, and "DP" is displayed 10 times on the screen instantly. Although, the script should first wait for 5 seconds and then display "DP"... then again wait for 5 seconds and display "DP"..... and so on.... for 10 times.

    Does anyone know where the problem is? It will be really appeciated :)
     
    netaddict, Aug 16, 2006 IP
  2. e_hippie

    e_hippie Guest

    Messages:
    32
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Try this:
    
    <?php
    for($i=1;$i<=10;$i++)
    	{
    	echo 'DP<br>';
    	flush();
    	sleep(5);
    	}
    ?>
    
    PHP:
    or, if you really want that initial 5 second delay:
    
    <?php
    for($i=1;$i<=10;$i++)
    	{
    	sleep(5);
    	echo 'DP<br>';
            flush();
    	}
    ?>
    
    PHP:


     
    e_hippie, Aug 16, 2006 IP
    netaddict likes this.
  3. netaddict

    netaddict Peon

    Messages:
    640
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks! It is working now the way I wanted :)
     
    netaddict, Aug 16, 2006 IP