Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

apache - PHP echo-ing content as page loads

So I'm doing some experimenting with PHP/Apache. Let's 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 webserver all the data appears in the browser at once, after all 4 sleep()s have been executed (8 seconds).

However if I run it on my host's server, the data is echo-ed to the browser in real time. As in... div1 appears, after 2 seconds div 2 appears etc.

Why is that? Is this some setting in Apache?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

No it may be a setting in php.

In you local server, output_buffering is enabled in your php.ini file.

You can disable it by setting :

output_buffering = off

To Ensure that the content is sent to the browser each time a echo-like statement is used, add :

implicit_flush = on

You also can set the buffer size by giving output_buffering a value.

output_buffering = 4096

here the buffer size would be 4KB.

Output buffering tells php to keep in memory all data to be sent to the browser until it encouters a flush() instruction in your code, the buffer happens to be full, or it is the end of the script.

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...