Buffer is a memory address space, the default size of the Linux system is generally 4096 (4KB). A region that is used primarily for transferring data between devices that are not synchronized at the same speed or with different priority devices.
By using a buffer, you can make the process of waiting for each other less.
For example, when you open a text editor and enter a character, the system does not write to the disk immediately. Instead, it is stored in buffer, and the contents of the buffer are written to disk when a buffer is filled.
Of course, you can also use flush to force data from the buffer to disk.
In PHP, such as Echo,print, the output is not immediately passed through TCP to the browser output. Instead, the data is written to PHP buffer. When a PHP buffer is full, it is passed to the browser via TCP.
Echo/print-> php output_buffer-> TCP buffer-> browser
PHP output_buffering
By default, PHP buffer is turned on, and the default value is 4096 (4KB). The output_buffering configuration can be found in php.ini. Know that the buffer is full, will not be sent to the browser.
You can also set Ob_start () so that even if the default value (4KB) is exceeded, it is not sent to the browser immediately, only to the completion of the script, or the Ob_end_flush method is invoked before it is exported to the browser.
1.output_buffering=4096, output less data (less than a buffer)
<?php for
($i =0 $i <10; $i + +) {
echo $i. ' <br> ';
Sleep (2);
}
? >
Run Result: When all scripts have been run, the output will not be finished, because the data is not full of buffer size.
2.output_buffering=4096, output less data (less than a buffer), close output_buffering, modify PHP.ini output_buffering=0
<?php
for ($i =0 $i <10; $i + +) {
echo $i. Str_repeat (" ", 500). ' <br> '; When you receive 256 (or more) bytes to begin displaying the page, you must send some extra space to allow the browser to display the page content.
flush ();
Sleep (1);
}
? >
Run results: Because the PHP buffering is disabled, you do not have to wait until the script is finished to output, the data does not stay in PHP buffer, you can see intermittent output. echo-> TCP buffer->browser
3. When output_buffering=4096, output large data (greater than a buffer), do not use Ob_start ()
<?php for
($i =0 $i <10; $i + +) {
echo file_get_contents (' F.txt '). $i. ' <br> ';
Sleep (1);
}
? >
Run Result: F.txt is a file larger than 4KB, because it is larger than the buffer default value, buffer space is not enough, whenever full a buffer will output, so you can see intermittent output.
4. When output_buffering=4096, output large data (greater than a buffer), use Ob_start ()
<?php
Ob_start ();
For ($i =0 $i <10; $i + +) {
echo file_get_contents (' F.txt '). $i. ' <br> ';
Sleep (1);
}
? >
Run result: Because the Ob_start () is used, the buffer is set to a large enough space, so it is saved until the script finishes execution.