• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1579
  • Last Modified:

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.
0
alateos
Asked:
alateos
1 Solution
 
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
 
AlexanderRCommented:
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
AlexanderRCommented:
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
 
AlexanderRCommented:
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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now