Buffer
Buffer is a memory address space, the Linux system default size is generally 4096 (4KB), that is, a memory page. It is primarily used for data transfer between devices that are not synchronized at the storage speed or between devices with different priority levels. By using buffer, the process can wait for each other to get smaller. Here's a popular example, when you open a text editor to edit a file, you input a character, the operating system will not immediately write this character directly to the disk, but first write to the buffer, when the full of a buffer, the data in the buffer will be written to disk, Of course, when calling kernel function flush (), it is mandatory to write the dirty data in the buffer back to disk.
Similarly, when executing echo,print, the output is not immediately transmitted via TCP to the client browser, but writes the data to PHP buffer. The PHP output_buffering mechanism means that a new queue is established before TCP buffer, and the data must pass through the queue. When a PHP buffer is full, the script process passes the output data from the PHP buffer to the system kernel to be displayed by TCP to the browser. So, the data will be written to these places in turn echo/pring-> PHP buffer-> TCP buffer-> browser
PHP output_buffering
By default, PHP buffer is turned on, and the buffer default is 4096, or 4KB. You can find the output_buffering configuration in the php.ini configuration file. When Echo,print output user data, the output data will be written to the PHP output_buffering until Output_ The buffering is full, sending the data over TCP to the browser display. You can also manually activate the PHP output_buffering mechanism by Ob_start (), so that even if the output exceeds 4KB data, it does not really send the data to TCP to the browser, because Ob_start () the PHP buffer space set to large enough. The data is sent to the client browser only until the end of the script, or when the Ob_end_flush function is invoked.
1. When output_buffering=4096, and output less data (less than a buffer)
Copy Code code as follows:
<?php
for ($i = 0; $i < $i + +) {
Echo $i. ' <br/> ';
<a href= "Http://www.php.net/sleep" >sleep</a> ($i + 1); //
}
?>
Phenomenon: Not every few seconds there will be intermittent output, but until the response to the end, to see the output at a time, waiting for the server script processing end, the browser interface remains blank. This is because the amount of data is too small, PHP output_buffering is not full. The order in which the data is written, followed by echo->php buffer->tcp Buffer->browser
2. When output_buffering=0, and output less data (less than a buffer)
Copy Code code as follows:
<?php
Through Ini_set (' output_buffering ', 0) does not take effect
Should edit/etc/php.ini, set output_buffering=0 disable output buffering mechanism
Ini_set (' output_buffering ', 0); Disable the output buffering feature completely
for ($i = 0; $i < $i + +) {
Echo $i. ' <br/> ';
<a href= "Http://www.php.net/flush" >flush</a> (); Notify the bottom of the operating system to send data to the client browser as soon as possible
<a href= "Http://www.php.net/sleep" >sleep</a> ($i + 1); //
}
?>
Phenomenon: Not consistent with the previous display, disabling the PHP buffering mechanism, in the browser can be intermittent to see the intermittent output, do not have to wait until the script finished to see the output. This is because the data is not stuck in the PHP output_buffering. The order in which the data is written is, in turn, Echo->tcp buffer->browser
3. When output_buffering=4096., the output data is greater than a buffer and does not call Ob_start ()
Create a file of 4KB size
$dd If=/dev/zero of=f4096 bs=4096 count=1
Copy Code code as follows:
<?php
for ($i = 0; $i < $i + +) {
echo <a href= "http://www.php.net/file_get_contents" >file_get_contents</a> ('./f4096 '). $i. ' <br/> ';
<a href= "Http://www.php.net/sleep" >sleep</a> ($i + 1);
}
?>
Phenomenon: The response is not finished (HTTP connection is not closed), intermittent output can be seen intermittently, the browser interface will not remain blank. Although the PHP output_buffering mechanism is enabled, it will still be intermittent output, rather than one-time output, because output_buffering space is not enough. Each full PHP buffering, the data is sent to the client browser. 4. When output_buffering=4096, the output data is greater than a TCP buffer, call Ob_start ()
Copy Code code as follows:
<?php
<a href= "Http://www.php.net/ob_start" >ob_start</a> (); Open PHP Buffer
for ($i = 0; $i < $i + +) {
echo <a href= "http://www.php.net/file_get_contents" >file_get_contents</a> ('./f4096 '). $i. ' <br/> ';
<a href= "Http://www.php.net/sleep" >sleep</a> ($i + 1);
}
<a href= "Http://www.php.net/ob_end_flush" >ob_end_flush</a> ();
?>
Phenomenon: Until the server-side script processing completed, the response ended, only to see the complete loss, the output interval time is very short, so that you do not feel a pause. Before the output, the browser maintains a blank interface, waiting for server-side data. This is because once PHP calls the Ob_start () function, it expands the PHP buffer to be large enough until the Ob_end_flush function call or the script runs a node to send data from PHP buffer to the client browser.
tcpdump observation
Here, we monitor the TCP message via tcpdump to observe a difference between using Ob_start () and not using it.
1. No use of Ob_start ()
12:30:21.499528 IP 192.168.0.8.webcache > 192.168.0.28.cymtec-port:. Ack 485 win 6432 12:30:21.500127 IP 192.168.0.8.webcache > 192.168.0.28.cymtec-port:. 1:2921 (2920) Ack 485 win 6432 12:30:21.501000 IP 192.168.0.8.webcache > 192.168.0.28.cymtec-port:. 2921:7301 (4380) Ack 485 win 6432 12:30:21.501868 IP 192.168.0.8.webcache > 192.168.0.28.cymtec-port:p 7,301:8,412 (1111) ACK 485 win 643 12:30:24.502340 IP 192.168.0.8.webcache > 192.168.0.28.cymtec-port:. 8412:14252 (5840) Ack 485 win 6432 12:30:24.503214 IP 192.168.0.8.webcache > 192.168.0.28.cymtec-port:. 14252:15712 (1460) Ack 485 win 6432 12:30:24.503217 IP 192.168.0.8.webcache > 192.168.0.28.cymtec-port:p 15,712:16,624 (9 Ack 485 win 6432 12:30:31.505934 IP 192.168.0.8.webcache > 192.168.0.28.cymtec-port:. 16624:23924 (7300) Ack 485 win 6432 12:30:31.506839 IP 192.168.0.8.webcache > 192.168.0.28.cymtec-port:p 23,924:24,836 (9 () Ack 485 win 6432 12:30:42.508871 IP 192.168.0.8.webcache > 192.168.0.28.cymtec-port:. 24836:32136 (7300) Ack 485 win 6432 12:30:42.509744 IP 192.168.0.8.webcache > 192.168.0.28.cymtec-port:p 32,136:33,048 (9 Ack 485 win 6432 12:30:57.512137 IP 192.168.0.8.webcache > 192.168.0.28.cymtec-port:. 33048:40348 (7300) Ack 485 win 6432 12:30:57.513016 IP 192.168.0.8.webcache > 192.168.0.28.cymtec-port:p 40,348:41,260 (9 Ack 485 win 6432 12:31:06.513912 IP 192.168.0.8.webcache > 192.168.0.28.cymtec-port:p 41260:41265 (5) ACK 485 win 6 432 12:31:06.514012 IP 192.168.0.8.webcache > 192.168.0.28.cymtec-port:f 41265:41265 (0) Ack 485 win 6432 12:31:06.5143 IP 192.168.0.8.webcache > 192.168.0.28.cymtec-port:. ACK 486 win 6432
2. The use of Ob_start ()
12:36:06.542244 IP 192.168.0.8.webcache > 192.168.0.28.noagent:. Ack 485 win 6432 12:36:51.559128 IP 192.168.0.8.webcache > 192.168.0.28.noagent:. 1:2921 (2920) Ack 485 win 6432 12:36:51.559996 IP 192.168.0.8.webcache > 192.168.0.28.noagent:. 2921:7301 (4380) Ack 485 win 6432 12:36:51.560866 IP 192.168.0.8.webcache > 192.168.0.28.noagent:. 7301:11681 (4380) Ack 485 win 6432 12:36:51.561612 IP 192.168.0.8.webcache > 192.168.0.28.noagent:. 11681:16061 (4380) Ack 485 win 6432 12:36:51.561852 IP 192.168.0.8.webcache > 192.168.0.28.noagent:. 16061:20441 (4380) Ack 485 win 6432 12:36:51.562479 IP 192.168.0.8.webcache > 192.168.0.28.noagent:. 20441:24821 (4380) Ack 485 win 6432 12:36:51.562743 IP 192.168.0.8.webcache > 192.168.0.28.noagent:. 24821:29201 (4380) Ack 485 win 6432 12:36:51.562996 IP 192.168.0.8.webcache > 192.168.0.28.noagent:. 29201:33581 (4380) Ack 485 win 6432 12:36:51.563344 IP 192.168.0.8.webcache > 192.168.0.28.noagent:p 33,581:35,041 (1460) Ack 485 win 6432 12:36:51.563514 IP 192.168.0.8.webcache > 192.168.0.28.noagent:. 35041:36501 (1460) Ack 485 win 6432 12:36:51.563518 IP 192.168.0.8.webcache > 192.168.0.28.noagent:. 36501:37961 (1460) Ack 485 win 6432 12:36:51.563523 IP 192.168.0.8.webcache > 192.168.0.28.noagent:. 37961:39421 (1460) Ack 485 win 6432 12:36:51.563526 IP 192.168.0.8.webcache > 192.168.0.28.noagent:. 39421:40881 (1460) Ack 485 win 6432 12:36:51.563529 IP 192.168.0.8.webcache > 192.168.0.28.NOAGENT:FP 40,881:41,233 (352) Ack 485 win 6432 12:36:51.570364 IP 192.168.0.8.webcache > 192.168.0.28.noagent:. ACK 486 win 6432
Through the above comparison, we can see that the data packet time interval is obviously different. No use of Ob_start (), the time interval is relatively large, waiting about 4 seconds to send the data in the TCP buffer sent out. The data was not stuck in the PHP buffer for a long time and the output data was sent to the client browser. This is because, soon the PHP buffer is written full, have to send out the data. When Ob_start () is enabled, the packet is sent to the client at almost the same time. This makes it possible to infer that the data has been stuck in the PHP buffer until the Ob_end_flush () is invoked to send the data in the PHP buffer to the client browser.
Output buffering function
1.ob_start
Activates the output_buffering mechanism. Once activated, the script output is no longer directed to the browser, but is temporarily written to the PHP buffer memory area.
PHP opens the output_buffering mechanism by default, except that by invoking the Ob_start () function, the output_buffering value is extended to large enough. You can also specify $chunk_size to specify the value of Output_buffering. The default value for $chunk _size is 0, which means that the data in PHP buffer will not be sent to the browser until the script is finished running. If you set the size of the $chunk_size, the data in buffer will be sent to the browser as long as the length of the data in the buffer reaches the value.
Of course, you can process the data in the buffer by specifying $ouput_callback. For example, function Ob_gzhandler, compressed data in the buffer and then sent to the browser.
2.ob_get_contents
Gets a copy of the data in the PHP buffer. It is worth noting that you should call this function in the Ob_end_clean () function call, otherwise ob_get_contents () returns an empty character.
3.ob_end_flush and Ob_end_clean
These two functions are somewhat similar and will close the ouptu_buffering mechanism. But the difference is that Ob_end_flush simply flushes the data in the PHP buffer (flush/send) to the client browser, while Ob_clean_clean empties the data in the PHP bufeer (erase), but does not send it to the client browser. After the Ob_end_flush call, the data in the PHP buffer still exists, and ob_get_contents () can still get a copy of the data in the PHP buffer. After the Ob_end_clean () call Ob_get_contents () takes an empty string, and the browser does not receive the output, that is, there is no output.
Idiomatic cases
Ob_start () is often seen in some template engines and paging file caches. In the well-known open source project Wordpress,drupal,smarty and so on places, all can discover their trace shadow. Here is the application of Drupal.
Template file
Copy Code code as follows:
@file: user-profile.tpl.php
<div>
<ul>
<li>username: <?php echo $user->name;?></li>
<li>picture:<?php Echo $user->picture;?></li>
</ul>
</div>
@file: template-render.php
<?php
function Theme_render_template ($template _file, $variables) {
if (!<a href= "Http://www.php.net/is_file" >is_file</a> ($template _file) {return "";}
<a href= "Http://www.php.net/extract" >extract</a> ($variables, Extr_skip);
<a href= "Http://www.php.net/ob_start" >ob_start</a> ();
$contents = <a href= "http://www.php.net/ob_get_contents" >ob_get_contents</a> ();
<a href= "Http://www.php.net/ob_end_clean" >ob_end_clean</a> ();
return $contents;
}
?>
@file:p rofile.php
<?php
$variables = <a href= "Http://www.php.net/array" >array</a> (' user ' => $user);
Print theme_render_template (' user-profile.tpl.php ', $variables);
?>