Implementing static PHP website pages based on ob series functions

coldplay.xixi
Release: 2023-04-09 08:04:01
forward
2112 people have browsed it

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:

";
}
?>
Copy after login

Staticization:

";
}
$str = ob_get_contents();
ob_end_clean();
$fp = fopen("test.html","w");
fwrite($fp,$str);
fclose($fp);
echo "

success write

"; ?>
Copy after login

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.

2,

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.

3,

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.

4,

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.

5,

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).

6,

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!

7,

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:

PHP programming from entry to proficiency

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!

Related labels:
source:jb51.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact [email protected]
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!