For details about the PHP Cache mechanism OutputControl, outputcontrol. For details about the PHP Cache mechanism OutputControl, in the configuration of php5.2, output_buffering is disabled by default. Therefore, a warning will appear when you run the following three lines of code: Output Control of the PHP Cache mechanism, outputcontrol
In php5.2 configuration, output_buffering is disabled by default. Therefore, a warning is displayed when you run the following three lines of code:
Warning: Cannot modify header information-headers already sent
echo 'hello1';header('content-type:text/html;charset=utf-8');echo 'hello2';
You can enable OB cache in either of the following ways:
1. enable output_buffering = 4096 in php. ini
With this command enabled, every PHP script calls the ob_start () function at the beginning. output_buffering = 4096 is enabled by default in PHP5.5.
2. directly use ob_start () in the program ();
Enable the output buffer. When the output buffer is activated, the script does not output content (except the http header). On the contrary, the content to be output is stored in the internal buffer.
The content of the internal buffer can be copied to a string variable using the ob_get_contents () function. To output the content stored in the internal buffer, you can use the ob_end_flush () function. In addition, using the ob_end_clean () function will silently discard the buffer content.
/*** Test with output_buffering = off */ob_start (); // enable echo 'hello1' in ob cache; // store the ob cache header ('content-type: text/html; charset = utf-8 '); // save it to the program cache // ob_end_clean (); // clear the ob cache and disable echo 'hello2' in the ob cache '; // store the ob cache $ str = ob_get_contents (); // return the data stored in the obslow state (excluding the buffer content using file_put_contents('ob.txt ', $ str); // Save $ str to the file // ob_clean (); // clear the ob cache echo 'hello3'; // save it to the ob cache echo 'hello4'; // save it to the ob cache/* This script will generate the ob.txt file and save it to hello1hello2, the browser outputs no content in the hello1hello2hello3hello4 * // * release file, and the browser outputs no content in hello3hello4 * // * release. Because ob cache is disabled, the browser outputs hello2hello3hello4 */
Example of ob_flush () and ob_end_flush:
Ob_start (); echo 'abc'; // store the ob cache header ('content-type: text/html; charset = utf-8 '); // echo 'hello' in the program cache; // Save the ob_flush () in the ob cache; // output the content in the ob cache to the program cache and clear the ob cache, disable ob cache // ob_end_flush () // output the content in ob cache to the program cache, clear ob cache, and disable echo 'A' in ob cache '; // echo ob_get_contents ();/* output abchelloaaaa * // * comment ob_flush, open ob_end_flush, and output abchelloaa */
Note:
When output_buffering = 4096 is enabled, ob_end_clean () only closes the ob cache once (that is, the ob_start is enabled), and the system does not close.
The same is true for ob_end_flush.
Operating principles/principles of OB cache:
1. When ob cache is enabled, echo data is first put into ob cache
2. put the header information directly in the program cache.
3. when the page is executed to the end, the ob cached data is put in the program cache, and then returned to the browser once.
There is also a flush (); force refresh the PHP program cache to the browser cache.
Features: for some versions of Microsoft Internet Explorer, the page is displayed only after 256 bytes are received. Therefore, additional spaces must be sent to display the page content in these browsers.
Echo str_repeat ('', 1024); // repeated output of multiple characters (to solve the problem that the browser caches 256 bytes before outputting) for ($ I = 0; $ I <5; $ I ++) {echo $ I; flush (); // force refresh the program cache to the browser cache sleep (1); // sleep for 1 second, http connection not closed, $ I} output every 1 second}
Which Cache mechanism is the best to use in php;
This depends on your actual situation, including file cache, database cache, and memcache cache .....
What is the caching mechanism of php?
Mainly include:
① General cache technology ② page cache ③ time-triggered cache ④ Content-triggered cache ⑤ static cache (generate html files)
6. memory cache 7. php buffer 2. MYSQL cache 2. Web cache based on reverse proxy, DNS round-robin
However, it is generally used for ① ② ④. Others are websites with large data volumes and many interactions, which can be used to reduce server pressure.
Reference: blog.163.com/..44905/
In the configuration of php5.2, output_buffering is disabled by default. Therefore, a warning will appear when you run the following three lines of code :...