The difference between PHP flush () and Ob_flush () _php instance

Source: Internet
Author: User
Tags flush
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 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---ob_flush ()

By default, PHP buffer is turned on, and the buffer default is 4096, or 1kb. 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 1KB data, it does not really send the data to TCP to the browser, Because Ob_start () sets the PHP buffer space to be 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.

The use of these two functions, even if many people are most puzzled by the problem, the manual on the two functions of the explanation is vague, did not explicitly point out their differences, it seems that both of the function is to refresh the output cache. However, if Fush () is replaced with Ob_flush () in the code at the beginning of our article, the program can no longer execute correctly. Obviously, they are different, otherwise the manual also directly shows that one of them is another function of the alias can be, 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 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 in the waiting output state, and you do not send anything to the client directly using flush (). and the role of Ob_flush () is the original output cache content out, set to wait for the output state, but not directly to the client, then you need to use Ob_flush () and then use Flush (), the client can immediately obtain the output of the script.

the correct sequence of flush and ob_flush, correct should be, first Ob_flush flush again, as follows:
Ob_flush ();
Flush ();
If the Web server's operating system is a Windows system, the order is reversed or the Ob_flush () does not appear to be problematic. [Pending verificationHowever, 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 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.
The third parameter: whether to erase the cache, optionally, the default is true, and if set to False, the cache will not be purged until the execution of the script completes.
2.ob_get_contents
Gets a copy of the data in the PHP buffer. It is worth noting that you should call the function before the Ob_end_clean () function is called, otherwise ob_get_contents () returns an empty character.

You can use Ob_get_contents () to get the 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 server-side cached data without any data or other behavior.
The server-side cache is stacked, which means that you can open another cache Ob_start () in the Ob_start () before closing it.

But you also have to be sure to turn off the cached operation as much as the number of operations that turn on the cache.
Ob_start () can specify a callback function to handle cached data, and if a ob_start () is nested within another ob_start (), we assume that the outer ob_start (), the number is a, and the inner Ob_start () number is B, Each of them developed a callback function is Functiona and functionb, then in the cache B data output, it will ancestors funcitonb callback function processing, and then to the outer Functiona callback function processing, before the output to the client.

In addition, the manual said that for some Web servers, such as Apache, the use of callback functions may change the current working directory of the program, the solution is to manually modify the work directory back in the callback function, with the ChDir function, this seems not often encountered, encountered when you remember to check the manual it.

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.

before 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_flush () call Ob_get_contents () takes an empty string, and the browser does not receive the output, that is, there is no output.

The

can use Ob_get_contents () as a string to get the data for the server-side cache, and using Ob_end_flush () outputs the cached data and closes the cache. The
uses Ob_end_clean () to silently purge the server-side cached data without any data or other behavior.
Server-Side caching is stacked, which means that you can open another cache Ob_start () in the Ob_start () before shutting down. But you also have to be sure to turn off the cached operation as much as the number of operations that turn on the cache.
Ob_start () can specify a callback function to handle cached data, and if a ob_start () is nested within another ob_start (), we assume that the outer ob_start (), the number is a, and the inner Ob_start () number is B, Each of them developed a callback function is Functiona and functionb, then in the cache B data output, it will ancestors funcitonb callback function processing, and then to the outer Functiona callback function processing, before the output to the client.

In addition, the manual says that for some Web servers, such as Apache, the use of callback functions may alter the current working directory of the program, and the solution is to manually modify the working directory back in the callback function, using the CHDIR function, which seems to be infrequently encountered, Remember to check the manual when you meet.

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.