Php page cache mainly uses ob functions, such as ob_start (), ob_end_flush (), ob_get_contents (), today we will talk about using these functions to achieve static php website pages and convert PHP execution pages into HTML in advance. this is one of the so-called static PHP methods.
Other methods include template replacement and opcache.
Static functions:
Improves website response speed and reduces server load;
SEO for search engines;
Using the PHP output buffer to directly write html, and regularly scan the website in the background, it is easier to achieve static web page.
Before static:
<?phpfor($i=0;$i<1000;$i++){ echo "bull
";}?>
Static:
<?phpob_start();for($i=0;$i<1000;$i++){ echo "bull
";}$str = ob_get_contents();ob_end_clean();$fp = fopen("test.html","w");fwrite($fp,$str);fclose($fp);echo "success write
";?>
The code will first become a test.html. to put it bluntly, it is to exchange space for time. for news websites, the published content will not change after it is basically written, and it is more suitable for static html conversion.
The user can directly access this html page when accessing it.
Use apache benchmark to test the effect:
AB .exe-n1000-c10
The difference between the two is at least 90% ms in connections over 150. Compared with complicated php, html saves a lot of computations.
Additional functions
1. Flush: refresh the buffer content and output it.
Function format: flush ()
Note: This function is frequently used and highly efficient.
2. ob_start: Open the output buffer.
Function format: void ob_start (void)
Note: When the buffer zone is activated, all non-file header information from the PHP program is not sent, but stored in the internal buffer zone. To output the buffer content, you can use ob_end_flush () or flush () to output the buffer content.
3. ob_get_contents: returns the content of the internal buffer.
Usage: string ob_get_contents (void)
Note: This function returns the content in the current buffer. if the output buffer is not activated, FALSE is returned.
4. ob_get_length: return the length of the internal buffer.
Usage: int ob_get_length (void)
Note: This function returns the length of the current buffer. it is the same as ob_get_contents if the output buffer is not activated. Returns FALSE.
5. ob_end_flush: sends the content of the internal buffer to the browser and closes the output buffer.
Usage: void ob_end_flush (void)
Note: This function sends the content of the output buffer (if any ).
6. ob_end_clean: delete the content of the internal buffer and disable the internal buffer.
Usage: void ob_end_clean (void)
Note: This function will not output the content of the internal buffer but delete it!
7. ob_implicit_flush: enable or disable absolute refresh
Usage: void ob_implicit_flush ([int flag])
Note: Anyone who has used Perl knows the meaning of $ | = x. This string can enable/disable the buffer, while the ob_implicit_flush function is the same as that. the buffer is disabled by default, after the absolute output is enabled, each script output is directly sent to the browser, and you do not need to call flush ().