On output_buffering,phpoutputbuffering_php Tutorial in PHP

Source: Internet
Author: User

On output_buffering,phpoutputbuffering in PHP


First, we have to say what the cache in PHP is probably!

In PHP, we can roughly divide the cache into client-side caches (browser caches), server-end caches (servers cache). Since PHP is based on B/s architecture, we can understand the server-side cache as the browser-side cache.
In the server-side PHP comes with the cache, can be divided into two major categories of cache! Program cache and OB cache! This is also the main content of our learning server-side cache!

The order in which the output is cached in PHP is:
PHP output cache turned on: echo,print, PHP output_buffering, server buffering, browser buffering display

PHP output Cache is not open: Echo,print, server buffering, browser buffering, browser display

Browser output cache: IE 256Bytes, Chrome and Firefox 1000Bytes, only the output data reached this length or script end browser will be output data on the page.

Second, the server-side response process

A, the client sends the request response to the server side!
B, Apache server loaded PHP module, open the appropriate process (or thread) to run the corresponding PHP script page!
C, without opening the OB cache, the results of the run will all be put into the program cache, and then packaged to send to the browser! The browser renders the page, creating the last Web page we see!
D, when the OB cache is turned on, the results of the operation are put into the OB cache and the program cache, respectively, when the program runs to the last line, the data in the OB cache is brushed back into the program cache, and then packaged back to the browser! The browser renders the page and generates the Web page we see!

Third, the common use of OB cache!

A, output_buffering=4096, output less data (less than one buffer)

for ($i =0; $i <5; $i + +) {  echo $i. '
'; Sleep (1); }

Run result: After all scripts have been run, the output will not be completed because the data is not full of a buffer size.

b, output_buffering=4096, output less data (less than one buffer), close output_buffering, modify PHP.ini output_buffering=0

echo str_repeat ("", 1024);//here, repeat output a blank for ($i = 0; $i <5; $i + +) {echo $i. "
"; Flush (); Sleep (1); }

Run result: Because the OB is disabled, you do not have to wait until the script is finished to output, the data does not stay in the OB, you can see intermittent output intermittently. echo->browser Buffering-browser display

C, output_buffering=4096, output large data (greater than one buffer), do not use Ob_start ()

for ($i =0; $i <5; $i + +) {   echo file_get_contents (' F.txt '). $i. '



'; Sleep (2); }

Run Result: F.txt is a file greater than 4KB, because the buffer space is larger than the buffer default value, and will be output whenever a buffer is full, so you can see the intermittent output.

D, output_buffering=4096, output larger data (greater than one buffer), using Ob_start ()

Ob_start (); for ($i =0; $i <5; $i + +) {   echo file_get_contents (' F.txt '). $i. '



'; Sleep (2); }

Run Result: Because Ob_start () is used, the buffer is set to a sufficient amount of space, so it will be saved until the script finishes executing.

E, Output_buffering=on, using Ob_start ()

Ob_start (); echo "abc-"; Header ("Content-type:text/html;charset=utf-8"); echo "hello-"; Ob_end_flush (); echo "aa-"; Echo ob_get_contents ();

Running Result: abc-hello-aa-abc-hello-aa-

F, Output_buffering=off, using Ob_start ()

Ob_start (); echo "abc-"; Header ("Content-type:text/html;charset=utf-8"); echo "hello-"; Ob_end_flush (); echo "aa-"; Echo ob_get_contents ();

Running Result: abc-hello-aa-

The output buffers are stackable, which means that when a ob_start () is active, you can call another Ob_start (). Just make sure that the correct number of Ob_end_flush () is correctly called. If multiple output callback functions are active, the output will be filtered sequentially through them in nested order.

Note: In PHP5.2, the OB is turned off by default, and after 5.3 is turned on by default;

Common methods:

1.ob_start
Activating the output_buffering mechanism, once activated, the script is no longer output directly to the browser, but is temporarily written to the PHP buffering area. It is not sent until after the script has finished running.

2.ob_get_contents
Get the data in PHP buffering, note: To be called before Ob_end_clean (), otherwise only the null character will be obtained.

3.ob_end_flush and Ob_end_clean
Ob_end_flush will output the data in PHP buffering, but it will not be emptied.
Ob_end_clean will not output, only the data in PHP buffering will be emptied.

4.ob_flush, Flush, Ob_implicit_flush
Ob_flush refreshes the data in PHP buffering to the program cache

Flush will flush the program cache into the browser cache

Ob_implicit_flush will turn the absolute (implicit) swipe on or off. Absolute (implicit) brushing will result in a single brush operation after each output call, so that explicit calls to flush () are no longer required

Four, the role of OB cache!

OB cache in all aspects of the application, but I know the main is in two aspects!
A, when the site is ready to do the site static, choose OB Cache is a good choice!

b, solve Warning:cannot modify header information-headers already sent by error!
The reason why the error occurred: Because the response head and the corresponding body position dislocation caused! Normally, the server returns the appropriate content to the browser, which should be: response header + response body!

However, if we turn on the OB cache, then the corresponding header information (usually the header () function to set the information) will be put into the program cache!
Other output content, such as Echo Print_r Var_dump, will be put into the OB cache first!
When the program ends, or the OB cache is closed, the content of the OB cache is placed in the program cache! This ensures that the response header information is always in front of the response body content!

The above mentioned is the whole content of this article, I hope you can like.

http://www.bkjia.com/PHPjc/1031492.html www.bkjia.com true http://www.bkjia.com/PHPjc/1031492.html techarticle a brief talk on PHP output_buffering,phpoutputbuffering one, we have to say what the cache in PHP is probably! In PHP, we can roughly divide the cache into client-side caching (Browse ...

  • 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.