PHP echo content on page load

So, I'm experimenting with PHP / Apache. Say I have this code.

<div>DIV 1</div> <?php sleep(2); ?> <div>DIV 2</div> <?php sleep(2); ?> <div>DIV 3</div> <?php sleep(2); ?> <div>DIV 4</div> <?php sleep(2); ?> 

For some reason, on my local apache web server, all data is displayed in the browser at once, after all 4 sleep modes () have been completed (8 seconds).

However, if I run it on my host server, the data will be displayed in real time in the browser. As in ... div1 appears, after 2 seconds div 2 appears, etc.

Why? Is this some kind of setup in Apache?

+6
source share
1 answer

No, it can be setup in php.

On your local server, the output_buffering function is included in the php.ini file.

You can disable it by setting:

 output_buffering = off 

To ensure that the content is sent to the browser each time an echo-like operator is used, add:

 implicit_flush = on 

You can also set the buffer size by providing an output_buffering value.

 output_buffering = 4096 

here the buffer size will be 4 KB.

Output buffering tells php to store in memory all the data that should be sent to the browser, until it checks the flush () command in your code, the buffer is full, or this is the end of the script.

Here is the full link for the output buffer from php.net: php output buffer

+6
source

Source: https://habr.com/ru/post/890821/


All Articles