Home > Article > Backend Development > Implementing static PHP website pages based on ob series functions
Pre-converting the PHP execution page into HTML is one of the so-called PHP static methods.
Other methods include template replacement, opcache and other methods.
The role of staticization:
Improve the response speed of the website and reduce the load of the server;
Used for search engine SEO;
Use PHP output buffer to write directly into html, timing Scanning the website regularly in the background is a relatively easy way to achieve static web pages.
Before staticization:
<?php for($i=0;$i<1000;$i++){ echo "bull<br>"; } ?>
Staticization:
<?php ob_start(); for($i=0;$i<1000;$i++){ echo "bull<br>"; } $str = ob_get_contents(); ob_end_clean(); $fp = fopen("test.html","w"); fwrite($fp,$str); fclose($fp); echo "<p>success write</p>"; ?>
This code will pre-form a test.html. To put it bluntly, it actually exchanges space for time, like news For this kind of website, the published content will basically not change after it is written, so it is more suitable to be statically converted into HTML.
When users visit, they just need to direct to this html page.
Simply use apache benchmark to test the effect:
ab.exe -n1000 -c10
## The two are above 90% During the connection, the difference is at least 150ms. Compared with complex php, html still saves a lot of calculations.Attached is a brief introduction to related functions
1.Flush: Refresh the contents of the buffer and output.
Function format: flush()
Description: This function is frequently used and is very efficient.
ob_start : Open the output buffer
Function format: void ob_start(void)
Description: When the buffer is activated, all non-file headers from the PHP program None of the information is sent, but is saved in an internal buffer. In order to output the contents of the buffer, you can use ob_end_flush() or flush() to output the contents of the buffer.
ob_get_contents : Return the contents of the internal buffer.
Usage: string ob_get_contents(void)
Description: This function will return the contents of the current buffer. If the output buffer is not activated, it will return FALSE.
ob_get_length: Returns the length of the internal buffer.
Usage: int ob_get_length(void)
Description: This function will return the length in the current buffer; the same as ob_get_contents, if the output buffer is not activated. then returns FALSE.
ob_end_flush: Send the contents of the internal buffer to the browser and close the output buffer.
Usage: void ob_end_flush(void)
Description: This function sends the contents of the output buffer (if any).
ob_end_clean: Delete the contents of the internal buffer and close the internal buffer
Usage method: void ob_end_clean(void)
Description: This function will not output the internal The contents of the buffer but delete it!
ob_implicit_flush: Turn on or off absolute refresh
Usage method: void ob_implicit_flush ([int flag])
Description: Anyone who has used Perl knows $|= The meaning of ).
Related learning recommendations:
The above is the detailed content of Implementing static PHP website pages based on ob series functions. For more information, please follow other related articles on the PHP Chinese website!