PHP output buffering cache and program caching in-depth parsing _php tips

Source: Internet
Author: User
Tags flush
The following tests the OB cache and the program cache:
Before testing, we turned off the OB cache and set an obvious error level in the php.ini to test the results more clearly.
Output_buffering=off
Display_errors=on
Code 1:
Copy Code code as follows:

echo "PHP";
Header ("content-type:text/html;charset= ' Utf-8 '");
echo ' OK ';

You will receive
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:
Copy Code code as follows:

Ob_start ();
echo "PHP";
Header ("content-type:text/html;charset= ' Utf-8 '");
echo ' OK ';

The result is completely correct.

Reason Analysis:
Code 1:
PHP has sent a header message to the browser in Echo ' PHP '.
When it comes back
header ("content-type:text/html;charset= ' Utf-8 '");
Also see a header information, at this time has been called back to the header information, can not be more wrong.
Code 2:With the OB cache turned on, echo ' PHP ' puts the data you want to call the browser into the OB cache, then a header, and then the OB cache, which is then returned to the browser at the end of the page by the HTTP protocol to the program cache.
For a deeper understanding, look at the following code
Code 3:
Copy Code code as follows:

Ob_start ();
echo "PHP";
Header ("content-type:text/html;charset= ' Utf-8 '");
echo ' OK ';
Echo ' $ob =ob_get_contents ();
Echo $ob;

will output


Ob_get_contents () just gets the contents of the OB cache and doesn't know them.
Ob_get_contents () must be used before the OB cache is cleared
Code 4:

Copy Code code as follows:

Ob_start ();
echo "PHP";
Ob_clean ()//clear cached content but not close buffer, still can use (add things to)
Header ("content-type:text/html;charset= ' Utf-8 '");
echo ' OK ';
Echo ' $ob =ob_get_contents ();
Echo $ob;

Results:


Code 5:

Copy Code code as follows:

Ob_start ();
echo "PHP";
Ob_end_clean ()//empty cached content and close buffer, ob_get_contents not get content
Header ("content-type:text/html;charset= ' Utf-8 '");
echo ' OK ';
Echo ' $ob =ob_get_contents ();
Echo $ob;

Results:


Code 6:

Copy Code code as follows:

Ob_start ();
echo "PHP";
Ob_end_flush ()//Send cache to the program cache and turn off 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 ()
Copy Code code as follows:

Ob_start ();
echo "PHP";
Ob_flush ()///Send OB cache to program cache without turning off OB cache
Header ("content-type:text/html;charset= ' Utf-8 '");
echo ' OK ';
Echo ' $ob =ob_get_contents ();
Echo $ob;

Results:


Ob_clean ()
Empty the OB cache content but not close
Ob_get_flush ()
Flush out cache to program cache, turn off OB cache
Code 8:

Copy Code code 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 caching
Code 9:
Copy Code code as follows:

Ob_start ();
Echo ' a ';
Flush ()///flush the OB cache to the program cache and then to the browser output without affecting the OB cache
Echo ob_get_contents ();
Aa

Code 10:
Copy Code code as follows:

Ob_start ();
Echo ' a ';
Ob_flush ();//Flush the OB cache to the program cache, there is no cached content in OB
echo "<br/>ob_con". ob_get_contents ();
A is in the normal output, OB is not content

Program Caching:
Code 11:
Copy Code code as follows:

echo str_repeat ("", 1024);//Some versions of Microsoft Internet Explorer only start displaying the page after 256 bytes received, so you must send some extra spaces to allow the browsers to display the page content.
For ($i =0 $i <5; $i + +) {
echo $i;
echo "<br/>";
Sleep (1);
Flush ();
}

Will output a number in a second.
If there is no flush (), all the output will be temporarily in the program cache, and so on in the overall return to the browser This example illustrates the program cache

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.