About PHP Buffers: Ob_star, ob_get_contents

Source: Internet
Author: User
Tags php script

PHP Ob_star ob_get_contents elaborate Rustic Floral

About PHP Buffers

Ob_start: Open the output buffer, when the buffer is activated, all non-header file information from the PHP program is not sent, but is saved in the internal buffer.

Ob_get_contents: Returns the contents of the internal buffer.
Ob_get_clean: Returns the contents of the internal buffer and closes the buffer (equivalent to ob_get_contents () and Ob_end_clean ()).
Ob_get_flush: Returns the contents of the internal buffer, closes the buffer, and then releases the contents of the internal buffer. Equivalent to Ob_end_flush () and returns the internal buffer contents.
Ob_get_lenght: Returns the length of the internal buffer, which returns false if the buffer is not activated.


Ob_clean: Deletes the contents of the internal buffer without closing the buffer, meaning that the output after the statement will continue to be added to the buffer.
Ob_flush: Frees the contents of the internal buffer and removes the contents of the internal buffer, but does not close the buffer.
Flush: Refreshes the output buffer, Ob_flush releases the content, and not the contents of the PHP buffer, all output to the browser.
Ob_end_clean: Deletes the contents of the internal buffer and closes the buffer.
Ob_end_flush: Frees the contents of the internal buffer and closes the buffer.
(This will be output only with flush)
The Ob_gzhandler:ob_start callback function, which compresses the contents of the buffer with gzip.
Ob_implicit_flush: Turns absolute refresh on or off, which is off by default. The so-called absolute refresh, when there are output statements, such as ECHO is executed, will send the output directly to the browser, and no longer need to call flush () or wait until the end of the script output.
Precautions:
Some Web server output_buffering default is 4069 characters or larger, that is, the output must reach 4069 character server to flush the output buffer, in order to ensure that flush is valid, it is best to have the following statement before the Ob_flush () function:
Print Str_repeat ("", 4096); To ensure that the output_buffering value is reached.
The Ob_* series function is an output buffer that operates on PHP itself, so Ob_flush only refreshes PHP's own buffer. And flush is the buffer that refreshes Apache. Therefore, the correct use of the two order is: first Ob_flush, and then flush. Ob_flush is to release the data from the PHP buffer, flush is to send the buffer inside/out all the data to the browser.
Don't mistake the Ob_start (), the output of the script Echo/print will never appear on the browser. Because the PHP script finishes running, it automatically refreshes the buffer and outputs the content.
First Ob_start () Flash otherwise error message:ob_flush () [Ref.outcontrol]: Failed to flush buffer. No buffer to flush.
We're talking about the use of Ob_start.
Ob_start ([string output_callback]): Here is set a callback function, after opening the buffer, all the output information is no longer sent directly out of the browser, but in the output buffer, you can use this callback function to process the output of the information.
Like what:
function test ($STR) {
Return Str_replace (' php100 ', ' haha ', $str);
}
Ob_start (' Test ');
echo ' Hello php100 ';
Ob_end_flush ();
In the example above, the output using echo () will be saved in the output buffer until Ob_end_flush () is called or the script is terminated, and the output information is processed by a custom handler (replacing the string inside) and returning the result.

Buffer----Flush ()
Buffer is a memory address space, the Linux system default size is generally 4096 (1KB), 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 to these places echo/pring. PHP Buffer---browser
PHP output_buffering---ob_flush ()
By default, PHP buffer is turned on, and the default value of this buffer is 4096, which is 1kb. 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 1KB 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.
The use of these two functions is even the most puzzling problem of many people, the manual on the interpretation of two functions are also vague, do not explicitly point out their differences, it seems that both of the function is to refresh the output cache. But in the code at the beginning of our article, if Fush () is replaced with Ob_flush (), the program will no longer execute correctly. Obviously, they are different, or else the manual directly indicates that one of the other functions is an alias, there is no need to explain separately. So what is the difference between them?


When the cache is not turned on, the contents of the script output are in the state of waiting for output on the server side, and flush () can send the content waiting for output to the client immediately.


When the cache is turned on, the contents of the script output are stored in the output cache, and there is no content waiting for the output state, and you directly use flush () without sending anything to the client. The function of Ob_flush () is to remove the contents of the output cache, set to wait for the output state, but not directly to the client, you need to use Ob_flush () and then flush () before the client can immediately get the output of the script.
The correct order of flush and ob_flush should be, first ob_flush and then flush, as follows:
Ob_flush ();
Flush ();
If the Web server's operating system is a Windows system, then the order is reversed or not using Ob_flush () does not occur. [Pending verification] but the output buffer cannot be refreshed on the Linux system.
Output buffering function
1.bool Ob_start ([Callback $output _callback [, int $chunk _size [, BOOL $erase]])
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.
The third parameter: whether to erase the cache, optional, the default is true, and if set to False, the cache will not be purged until the script execution is complete.
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.
You can use Ob_get_contents () to obtain server-side cached data as a string.
Using Ob_end_flush () outputs the cached data and closes the cache.
Using Ob_end_clean () silently clears the data cached by the server, without any data or other behavior.


The server-side cache is stacked, which means you can also open another cache Ob_start () within the Ob_start () after you turn it off.
But you also have to make sure that the cache is turned off as much as the number of open cache operations.
Ob_start () can specify a callback function to handle the cached data, if one ob_start () is nested inside another ob_start (), we assume that the outer ob_start (), the number is a, the inner layer of the Ob_start () number is B, Each of them developed a callback function of Functiona and FUNCTIONB respectively, then in cache b data output, it will be the predecessor FUNCITONB callback function processing, and then to the outer Functiona callback function processing, before output to the client.


In addition, the manual said that for some Web servers, such as Apache, in the use of callback functions may change the current working directory of the program, the solution is to manually modify the working directory in the callback function back, with the ChDir function, this seems not often encountered, when encountered remember to check the manual it.
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.
Before the Ob_end_flush call, the data in PHP buffer still exists, and ob_get_contents () can still get copies of the data in PHP buffer.
After the Ob_end_flush () call, Ob_get_contents () takes an empty string, and the browser receives no output, that is, no output.
You can use Ob_get_contents () to obtain data from the server-side cache as a string, using Ob_end_flush () to output cached data and close the cache.

Using Ob_end_clean () silently clears the data cached by the server, without any data or other behavior.

About PHP Buffers: Ob_star, ob_get_contents

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.