In php, the outputbuffering cache (php's own cache mechanism) is provided to control the output display sequence of the program ). If the Ob cache is enabled, the output must first be stored in the ob cache and then stored in the program cache. If it is not enabled, the system directly enters the program cache. after the program is executed, press
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 (); // clears the cached content but does not close the cache area. It can also be used (add something to it)
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. the 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 'A ';
Echo ob_get_contents ();
// Abchelloaaaa
2. ob_flush (), flush () and program cache
Code 9:
The code is as follows:
Ob_start ();
Echo 'A ';
Flush (); // fl the Ob cache to the program cache and then fl it to the browser output without affecting the ob cache.
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 contains no 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.