Jump to content

while ($i <= 100)


soma56

Recommended Posts

I'm new to PHP and for example purposes I've included this simple code:

 

$i = 1;
while ($i <= 10) {
    echo $i++; 
}

 

My question is simple. As my code is a little more complex it seems to load until the loop is true before "echoing" anything. Is there a function that allows me to "echo" each case as it's being created? My code seems to take "a while" to load and then echoes back everything at once.

Link to comment
Share on other sites

the point thorpe was touching on is  "Sever-side" verse "Client-side" scripts.  PHP runs on the server and the entire script is executed before its data is sent to the client-side, aka your browser.  If you want to have things happen on your browser's screen after the page has loaded from the server, you need to use a client-side script just as javascript.

Link to comment
Share on other sites

It is possible doing something like it, assuming your web server is not setup to always buffer the output and that your browser doesn't try to load the entire thing first.

 

<?php
header('Content-type: text/plain');

$i = 1;
while ($i <= 100) {
        echo $i++ . PHP_EOL;
        flush();
        ob_flush();
        usleep(500000);
}

 

Live example: http://files.degeberg.com/flush.php

 

This works for me in Firefox 3.6.3, but not in Chrome 6.0.422.0 dev.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.