Use OB series functions to implement PHP Web page static _php instance

Source: Internet
Author: User

Converting PHP's execution page to HTML is one of the so-called PHP static methods.

Other methods include template substitution, Opcache, and so on.

The role of Static:

Improve the response speed of the website, reduce the load of the server;
SEO used in search engines;
The use of PHP output buffer directly written HTML, timed backstage regularly sweep the site, is more easily implemented Web page static.

Before Static:

<?php for
($i =0 $i <1000; $i + +) {
 echo "bull<br>";
}
? >

Static:

<?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 "<div>success write</div>";
? >

This piece of code will be in advance to form a test.html, in fact, is to use space in exchange for time, such as news sites, published content is basically not changed, more suitable for static into HTML.

When the user accesses, direct to this HTML page is OK.

Simply test the effect with the Apache benchmark:

Ab.exe-n1000-c10

The two are at least 150ms in connection with more than 90% of the connections. Compared to the complexity of PHP, HTML savings in the amount of computation is still quite a lot.

Introduction to other related functions


1, Flush: Refresh the contents of the buffer, output.
function format: Flush ()
Description: 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 is activated, all non-file header information from the PHP program is not sent, but is saved in the internal buffer. 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.
Using method: String ob_get_contents (void)
Description: This function returns the contents of the current buffer and FALSE if the output buffer is not active.

4, Ob_get_length: Returns the length of the internal buffer.
How to use: int ob_get_length (void)
Description: This function returns the length of the current buffer, as ob_get_contents if the output buffer is not active. Returns FALSE.

5, Ob_end_flush: Send the contents of the internal buffer to the browser, and turn off the output buffer.
Usage method: 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 does not output the contents of the internal buffer but deletes it!

7, Ob_implicit_flush: Turn absolute refresh on or off
use method: void Ob_implicit_flush ([int flag])
Description: People who have used Perl know the meaning of $|=x, a string that can open/ The buffer is closed, and the Ob_implicit_flush function is the same as that, by default, the buffer is turned off, and when absolute output is turned on, each script output is sent directly to the browser and no longer needs to invoke flush ().

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.