Jump to content

PHP immediate output?


LemonInflux

Recommended Posts

Hello. I'm doing a project where I need PHP data to be outputted immediately. So for something like this (an example I took from php.net for the sake of demonstration):

 

<?php

echo date('h:i:s') . "\n";

sleep(10);

echo date('h:i:s') . "\n";

?>

 

Normally, this code outputs the 2 after 10 seconds. What I want is for the first one to be outputted instantly, then after 10 seconds the next one is. How is this done?

Link to comment
Share on other sites

Web servers are designed to output a requested web page and browsers are designed to render that web page. Due to several steps of buffering and compression that php, web servers, and browsers can do, you should not waste your time trying to get a single requested web page to operate this way.

 

You can use multiple http requests to operate this way, using AJAX. The main page is requested and displayed normally, then AJAX is used to make additional http requests to the web server to get and update the information on the page being displayed in the browser.

 

If you describe what you are actually trying to do, someone can provide specific direction on how to do it.

Link to comment
Share on other sites

I understand your question, but can't think of any solution (other than using AJAX). I guess PHP is just build that way - the whole script is parsed, and then any output is outputted at once. Same reason an error-free echo statement at the start of the script will not output if an fatal error is found later in the script.

Link to comment
Share on other sites

ah ok. What I'm trying to do is build an installer that has 'live' updates on progress. So, for example, it would have:

 

<?php

// Create a file code here...
echo "file has been created!";
// Write to file code here...
echo "file has been written to!";

?>

 

That sort of thing, so the user can see how the install is doing as it happens. How would this be done with AJAX?

Link to comment
Share on other sites

I am no expert on PHP but I guess this has to do with output buffering.

 

PHP accumulates all output and releases it when the buffer is either full or flushed, or the script is finished.

 

I suggest you read a little about output buffering in PHP.

I don't know enough about it to explain any more, but I'm confident that this will solve your problem.

 

Link to comment
Share on other sites

I tested the buffer method, but it doesn't work that way. Found this script at http://www.phpit.net/article/output-buffer-fun-php/:

 

<?php

// Enable output buffering
ob_start();

// Print some output
echo 'Hello World';

// Flush output buffer (thus sends 'Hello World' to the client)
ob_flush();

// Print some more output
echo 'Hi There';

?>

 

and tried to put in sleep(10) after the ob_flush(). Still it was all outputted after 10 seconds.

 

AJAX is not something you just copy/paste, so search for some tutorials and learn through experience. What you're looking to achieve can surely be done with AJAX.

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.