PHP buffer output_buffering use of _php tutorial

Source: Internet
Author: User
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 to store data between devices that are out of sync or between devices with different priority levels. By using buffer, you can make the process less of a mutual wait. Here is a popular example, when you open a text editor to edit a file, when you enter a character, the operating system does not immediately write this character directly to the disk, but the first write to buffer, when the buffer is filled with a buffer, the data will be written to disk, Of course, when the kernel function flush () is called, it is mandatory to write the dirty data in buffer back to disk.

Similarly, when executing a echo,print, the output is not immediately displayed via TCP to the client browser, but instead writes the data to PHP buffer. The PHP output_buffering mechanism means that a new queue is established before the TCP buffer, and the data must pass through the queue. When a PHP buffer is full, the script process will send the output data from PHP buffer to the system kernel to be displayed by TCP to the browser. So, the data will be written in turn to these places:echo/print, PHP buffer, TCP buffer, browser

PHP output_buffering
By default, PHP buffer is turned on, and the default value of this buffer is 4096, which is 4kb. You can find the output_buffering configuration in the php.ini configuration file. The output data is written to the PHP output_buffering when the user data is Echo,print, until Output_ Buffering is full, this data will be transmitted via TCP to the browser display. You can also manually activate the PHP output_buffering mechanism through Ob_start (), so that even if the output exceeds 4KB data, it does not really give the data to TCP to the browser, because Ob_start () set the PHP buffer space to large enough. Data is sent to the client browser only until the script finishes, or when the Ob_end_flush function is called.

1. When output_buffering=4096, and output less data (less than one buffer)
Copy the Code code as follows:
for ($i = 0; $i < $i + +) {
Echo $i. '
';
Sleep ($i + 1); //
}
?>

Phenomenon: Not every few seconds there will be intermittent output, but until the end of the response, in order to see the output at once, wait until the end of the server script processing, the browser interface remains blank. This is because the amount of data is too small and PHP output_buffering is not full. The order in which the data is written, in turn echo->php buffer->tcp buffer->browser

2. When output_buffering=0, and output less data (less than one buffer)
copy code code as follows:
!--? PHP //Via Ini_set (' output_buffering ', 0) does not take effect
//should be edited/etc/ PHP.ini, setting output_buffering=0 disables the output buffering mechanism
//ini_set (' output_buffering ', 0);//Disable output buffering function completely
for ($i = 0; $i < $i + +) {
echo $i. '
';
Flush ();//notify the operating system to the bottom, as soon as possible the data to the client browser
Sleep ($i + 1);//
}
?

Phenomenon: not consistent with the display just now, after disabling PHP buffering mechanism, The intermittent output can be seen intermittently in the browser, without having to wait until the script finishes to see the output. This is because the data is not stuck in PHP output_buffering. The order in which the data is written is echo->tcp Buffer->browser

3. When output_buffering=4096., output data is greater than one buffer, do not call Ob_start ()
copy code code is as follows:
#//creating a 4kb size file
$dd if=/dev/zero of=f4096 bs=4096 Count=1
!--? php for ($i = 0; $i < $i + +) {
echo file_get_contents ('./f4096 '). $i. '
';
Sleep ($i + 1);
}
?

Behavior: The response is not over (the HTTP connection is not turned off), intermittent output can be seen intermittently, and the browser interface will not remain blank. Although the PHP output_buffering mechanism is enabled, it still intermittently outputs, rather than a one-time output, because output_buffering space is not enough. With each 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 the Code code as follows:
Ob_start (); Open PHP Buffer
for ($i = 0; $i < $i + +) {
Echo file_get_contents ('./f4096 '). $i. '
';
Sleep ($i + 1);
}
Ob_end_flush ();
?>

Phenomenon: Until the server end of the script processing completed, the response is finished, only to see the full output, the time interval is very short, so that you do not feel the pause. Before the output, the browser kept a blank interface, waiting for server data. This is because, once PHP calls the Ob_start () function, it expands PHP buffer to be large enough to send the data in PHP buffer to the client browser until the Ob_end_flush function call or the script runs at the junction speed.

Output buffering function

1.ob_get_level
Returns the nesting level of the output buffering mechanism, which prevents templates from repeatedly nesting themselves.

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 calling 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 of $chunk _size is 0, which means that data in PHP buffer is not sent to the browser until the end of the script run. If you set the size of the $chunk_size, the data in buffer is sent to the browser as long as the data length in the buffer reaches that value.

Of course, you can work with the data in buffer by specifying $ouput_callback. For example, function Ob_gzhandler, compress the data in buffer and then pass it to the browser.

2.ob_get_contents
Get a copy of the data in PHP buffer. It is important to note that you should call the function before 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 a bit similar and will close the ouptu_buffering mechanism. But the difference is that Ob_end_flush simply flushes the data in PHP buffer (flush/send) to the client browser, and Ob_clean_clean empties the data in PHP bufeer (erase), but does not send it to the client browser. After the Ob_end_flush call, the data in PHP buffer still exists, and ob_get_contents () can still get a copy of the data in PHP buffer. After the Ob_end_clean () call, Ob_get_contents () takes an empty string, and the browser receives no output, that is, no output.

Usage Cases
often see Ob_start () used in some template engine and page file caches. Under Wet CI, load the template's program code:
Copy CodeThe code is as follows:
/*
* Buffer the output
*
* We buffer The output for both reasons:
* 1. Speed. You get a significant speed boost.
* 2. So, the final rendered template can be
* Post-processed by the output class. Why do we
* Need post processing? For one thing, in order to
* Show the elapsed page load time. Unless we
* Can intercept the content right before it's sent to
* The browser and then stop the timer it won ' t be accurate.
*/
Ob_start ();
If The PHP installation does not support short tags we ' ll
Do a little string replacement, changing the short tags
To standard PHP echo statements.
if (bool) @ini_get (' short_open_tag ') = = = FALSE and Config_item (' rewrite_short_tags ') = = TRUE)
{
Replace short mark
echo eval ('?> '. Preg_replace ("/;*\s*\?>/", ";?>", Str_replace (' }
Else
{
Include ($_ci_path); Include () vs include_once () allows for multiple views with the same name
}

Logging Debug Information
Log_message (' Debug ', ' File loaded: '. $_ci_path);
Return the file data if requested
if ($_ci_return = = = TRUE)
{
$buffer = Ob_get_contents ();
@ob_end_clean ();
return $buffer;
}
/*
* Flush the buffer ... or buff the flusher?
*
* In order to permit nested within
* Other views, we need to flush the content back out whenever
* We are beyond the first level of output buffering so
* It can seen and included properly by the first included
* template and any subsequent ones. oy!
*
*/
if (Ob_get_level () > $this->_ci_ob_level + 1)
{
Ob_end_flush ();
}
Else
{
Adding template content to the output stream
$_ci_ci->output->append_output (Ob_get_contents ());
Clear Buffer
@ob_end_clean ();
}

http://www.bkjia.com/PHPjc/327578.html www.bkjia.com true http://www.bkjia.com/PHPjc/327578.html techarticle buffer buffer is a memory address space, the Linux system default size is generally 4096 (4KB), that is, a memory page. Mainly used for storage speed is not synchronized between the device or the priority of different devices ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.