Phpoutputbuffering cache and program cache in-depth parsing _ PHP Tutorial

Source: Internet
Author: User
Phpoutputbuffering cache and program cache for in-depth resolution. Next we will test ob cache and program cache: to make the test more effective, we will first disable ob cache and set an obvious error level in php. ini. Output_bufferingoffDisplay_er The following tests ob cache and program cache:
To make the test more effective, disable the ob cache and set an obvious error level in php. ini.
Output_buffering = off
Display_errors = on
Code 1:

The code is as follows:

echo "php";header("content-type:text/html;charset='utf-8'");echo 'ok';


The
Php
Warning: Cannot modify header information-headers already sent by (output started at D: \ www \ apache \ htdocs \ test \ t2.php: 2) in D: \ www \ apache \ htdocs \ test \ t2.php on line 3
OK
Code 2:

The code is as follows:

ob_start(); echo "php"; header("content-type:text/html;charset='utf-8'"); echo 'ok';


The results are completely correct.

Cause analysis:
Code 1:
Php has sent a header to the browser when echo 'php,
When
Header ("content-type: text/html; charset = 'utf-8 '");
The header information is displayed again, and the above header information has been recalled. this error cannot be solved.
Code 2:When ob cache is enabled, echo 'php' puts the data to be sent to the browser in ob cache first, and then encounters a header information, which is also put into Ob cache, after the page ends, it is cached by the program based on the http protocol and then returned to the browser.
Let's look at the following code for better understanding:
Code 3:

The code is as follows:

ob_start();echo "php";header("content-type:text/html;charset='utf-8'");echo 'ok';echo '';$ob=ob_get_contents();echo $ob;


Output


Ob_get_contents () only obtains the content in the ob cache, but it is unclear about them.
Ob_get_contents () must be used before the ob cache is cleared
Code 4:

The code is as follows:

Ob_start (); echo "php"; ob_clean (); // clear the cached content but do not close the cache area. you can also use the header ("content-type: text/html; charset = 'utf-8' "); echo 'OK'; echo''; $ ob = ob_get_contents (); echo $ ob;


Result:


Code 5:

The code is as follows:

Ob_start (); echo "php"; ob_end_clean (); // clears the cached content and closes the cache. ob_get_contents cannot retrieve the content header ("content-type: text/html; charset = 'utf-8' "); echo 'OK'; echo''; $ ob = ob_get_contents (); echo $ ob;


Result:


Code 6:

The code is as follows:

Ob_start (); echo "php"; ob_end_flush (); // sends the cache to the program cache and closes the ob cache header ("content-type: text/html; charset = 'utf-8' "); echo 'OK'; echo''; $ ob = ob_get_contents (); echo $ ob;


Code 7: compare code 6 with ob_flush ()

The code is as follows:

Ob_start (); echo "php"; ob_flush (); // sends the Ob cache to the program cache without disabling the ob cache header ("content-type: text/html; charset = 'utf-8' "); echo 'OK'; echo''; $ ob = ob_get_contents (); echo $ ob;



Result:


Ob_clean ()
Clear ob cache content but not close
Ob_get_flush ()
Clears the cache to the program cache and closes the ob cache.
Code 8:

The code is as follows:

Ob_start();echo 'abc';header("content-type:text/html;charset='utf-8'");echo 'hello';Ob_flush();echo 'aa';echo ob_get_contents();//abchelloaaaa


2. ob_flush (), flush () and program cache
Code 9:

The code is as follows:

Ob_start (); echo 'A'; flush (); // flush Ob cache to program cache and then flush to browser output, without affecting echo ob_get_contents (); // aa

Code 10:

The code is as follows:

Ob_start (); echo 'A'; ob_flush (); // clears the Ob cache to the program cache, and the ob does not have the cached content echo "ob_con ". ob_get_contents (); // a is output normally, and there is no content in Ob

Program cache:
Code 11:

The code is as follows:

Echo str_repeat ("", 1024); // for some versions of Microsoft Internet Explorer, this page is displayed only after 256 bytes are received, therefore, some extra spaces must be sent to display the page content in these browsers. For ($ I = 0; $ I <5; $ I ++) {echo $ I; echo ""; sleep (1); flush ();}


A number is output in one second.
If there is no flush (); all the output will be saved in the program cache first, and the entire result will be returned to the browser. This example shows the program cache.


To make the test more effective, disable the ob cache and set an obvious error level in php. ini. Output_buffering = off Display_er...

The above is the Php output buffering cache and program cache in-depth parsing _ PHP Tutorial content. For more information, please follow the PHP Chinese network (www.php1.cn )!

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.