How to create timed events in php

I'll ask this question in a very easy way. I need the php page to echo "hello" every 10 seconds.
alateosAsked:
Who is Participating?
 
AlexanderRConnect With a Mentor Enterprise Web DeveloperCommented:
Unfortunatly because like DelTreme stated, php sends output to browser on full completion of the script.  So while server processes the page (even something like counting time) you will never get output.

Clien side javascript can do that.This will refresh page for you every 10 seconds
function reloadFunction() {
    location.href = '<?php echo $_SERVER['PHP_SELF'];?>';
}
onLoad="setTimeout('reloadFunction()',10000)"
0
 
DelTremeCommented:
while (true)
{
  echo 'hello';
  sleep(10);
}

Note that if you are running the script in a browser window, you will never get output, as it sends it after the script is completed. For PHP CLI this works.
0
 
alateosAuthor Commented:
Didn't work for me... I just need the output to come on the browser every 10 seconds.

<?php
while (true)
{
$i = $i + 1
  echo $i;
  sleep(10);
}
?>
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

 
AlexanderREnterprise Web DeveloperCommented:
Sorry, its
<body onLoad="setTimeout('reloadFunction()',10000)">
0
 
alateosAuthor Commented:
So you're basically saying that on the php side alone it's impossible
0
 
AlexanderREnterprise Web DeveloperCommented:
No, php is desigened to parse the whole file at ONCE in its entirety.
0
 
UtteCommented:
you can just do a flush() on php side and it will instantly transmit...
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.