Php buffer why \ r \ n

I have a few conceptual questions (all related, I think) regarding the following script in the comments. The script works fine.

<?PHP ob_start(); // Create string to overflow browser buffer ...? $buffer = str_repeat(" ", 4096); // Indicate new header / html content ...? $buffer .= "\r\n<span></span>\r\n"; for ($i=0; $i<5; $i++) { echo $buffer.$i; ob_flush(); flush(); sleep(1); } ob_end_flush(); ?> 

First, why should I send \r\n<tag>\r\n to the browser? I guess this has something to do with the headlines.

Secondly, why do I need HTML code in the middle?

Thirdly, there are many examples that use 256 bytes instead of 4096. However, the script does not work if I use 256. Are these examples obsolete and will this number change again in the future?

// CHANGE SOURCES OF SOURCES

This code was compiled mainly from the comment in the php.net sleep() function and the solution to this SO issue . It is not mentioned why include \r\n .

// EDIT FOR MANAGERS

If I do not add \r\n , the HTML tag and the second set \r\n , the script will not execute correctly in Chrome or Safari (it just unloads all the values ​​at once).

In addition, if it is called before session_start() , it gives an error message: "Cannot send session cache limiters that have already been sent."

+3
php header output-buffering
Nov 16 '10 at 5:00
source share
3 answers

First, why should I send \r\n<tag>\r\n to the browser? I guess this has something to do with the headlines.

Secondly, why do I need HTML code in the middle?

Typically, the browser has to wait until they select the entire response until it is displayed (just think of XML, which can be valid until the last character). But since this will create a bad user experience, most browsers will begin to analyze and display the content as early as possible.

And this HTML snippet could be the initiator for the browser to actually create the DOM and start rendering.

Thirdly, there are many examples that use 256 bytes instead of 4096. However, the script does not work if I use 256. Are these examples obsolete and will this number change again in the future?

As the management hints that there may be some kind of additional buffering built into the web server, it may be an attempt to overflow those buffers that they also flushed to have the expected effect.

+2
Nov 16 '10 at 6:44
source share

The reason for using \r\n would be to make the rendering output beautiful when viewed using a Windows viewer such as notepad.exe .

There is nothing to do with the headlines.

Having seen that the code uses output buffering functions, I have no idea why they think that you need to try and overflow the 4kb buffer (by default in standard php.ini , although more professionals will prefer the lack of default output buffering).

+1
Nov 16 '10 at 5:29
source share
 <?php if (ob_get_level() == 0) ob_start(); for ($i = 0; $i<10; $i++){ echo "<br> Line to show."; echo str_pad('',4096)."\n"; ob_flush(); flush(); sleep(2); } echo "Done."; ob_end_flush(); ?> 
-one
Jun 30 '14 at 19:32
source share



All Articles