Print out 10 numbers one by one

I’m trying to print out from 1 to 10 one by one by the following script:

for($i=1; $i<=10; $i++){
echo “$i<br />”;
sleep(1); //Add or remove this is the same
}

I’m expecting to see it will print out 1, 2, …, 10 on the browser one by one. However, it will just wait for localhost, and then print out the list in one time.

How to print out the numbers one by one instead of one time?

php is executed server side, so you’ll have to do it client side using javascript.

or if you must get the numbers from the server then you can use ajax

Definitely php can do it on server side! Experts please help, thanks a lot!

if you want a time delay between each number then you will have to do it with plain vanilla javascript or ajax.

By server-side, Kalon means that its processed before being displayed in the browser. Therefore, by using the ‘sleep’ function, PHP is waiting, then continuing, but it won’t actually output anything to the browser until it’s finished.

Therefore you’ll need to use something client-side such as Javascript, or an ajax call to the PHP script.

it appears you might be a little confused about how php works in the overall scheme of things.

let me summarise:

  1. Say you have a plain vanilla html file with some optional javascript and css.

When someone points to that page on your server, the web server sends the html file back to the requestors browser and the browser processes the html, javascript and css and eventually displays your web page’s content in their browser

  1. Say you now have some php code as well in the same html file as in 1).

The file should now have a .php extension. This .php extension tells the web server that when someone points their browser to that .php file, the server should first send that php file to the php “engine” on the server. The php engine then reads through that php file line by line and ouputs any html, js,css code unaltered and processes any php code between <?php ?> tags as it comes across them. Any output from the php code is then inserted in place of the php code in the original php file. When the entire .php file has been read and processed by the php engine, the resultant output , which is now just plain html with the original js, css and output from the php code is sent back to the requestors browser and processed by the browser as in 1).

This works as expected on Firefox, but don’t ask me why though… :stuck_out_tongue:


<?php
ob_end_clean();
ob_implicit_flush(true);
foreach(range(1, 10) as $num){
  printf("%d<br />\
", $num);
  sleep(1);
}

I’m going to assume other browsers don’t work because of the unknown content length, feel free to educate me.

Adding the content length manually appears to make it work in IE8 at least.


<?php
header('Content-Length: 81');
ob_end_clean();
ob_implicit_flush(true);
foreach(range(1, 10) as $num){
  printf("%d<br />\
", $num);
  sleep(1);
}

I test this on my computer which is windows system, it’s working both on IE and Firefox without the header(‘Content-Length: 81’);

However, it’s not working after uploading to my linux server. You can check it here: http://www.cheapsubmit.net/test.php

Any suggestions?

It works in FireFox 3.6.13 here.

OK - I’ll do it - I’ll ask the obvious question - WHY?

Why do you want PHP to do a countdown (or up) with a delay?
What purpose will this serve?
What are you REALLY trying to achieve?

PS - in Google Chrome that page just waits then renders in one go.

It maybe work in the first time, but if you refresh the page, it will not.

It can be done with a time delay using the sleep function.

are you saying that was LSC-Rob said is not true then?