1

I have a html page that runs several consecutive php/bash scripts, and I would like to make the result visible on the page, before the next script starts. Is that possible?

Example:

<html> <body>
<br>Html1<br>
<?php
echo "A";
sleep(4);
echo "B";
?>
<br>Html2<br>
<?php
echo "C";
sleep(4);
echo "D";
?>
<br>Html3<br>
</body> </html>

...here nothing is shown before everything is done.

Steve Robillard
  • 13,445
  • 3
  • 36
  • 32
hpekristiansen
  • 1,030
  • 3
  • 17
  • 35

1 Answers1

2

This is probably a buffering issue.

Do flush() between consecutive echos to flush the PHP's internal output buffers.

Note though that your webserver may do additional buffering and so can the browser, so it is not really guaranteed that the changes will be displayed in browser.

If you want to test if other buffering is involved, you can post a long HTML comment after each output.

For example:

<?php
  echo "<!-- aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ... aaa -->";
?>

If the comment is long enough it will fill the buffer, forcing it to flush.

Please note that this is ONLY for testing, do NOT use that in production code.

  • That won't do it. (It doesn't show up even when output buffering is disabled.) Did you mean: [`flush()`](http://php.net/flush)? – Ry- May 06 '12 at 14:19
  • @minitech it should be enabled – KBN May 06 '12 at 14:20
  • Well PHP buffers internaly even without using `ob_` functions –  May 06 '12 at 14:21
  • You are right, I meant `flush()` but from my experience `ob_flush()` implicitly calls the `flush()`, so it's kinda the same. –  May 06 '12 at 14:26
  • No it is not working :o( - tried to add `flush()` to the example, but it made no difference. – hpekristiansen May 06 '12 at 14:43