The page cache saves the page to a file. The file is called directly next time you read it without querying the database. Here we will introduce how to use ob_start.
Example
| The Code is as follows: |
Copy code |
|
<? Php
Ob_start (); // open the buffer
Phpinfo (); // use the phpinfo Function
$ Info = ob_get_contents (); // obtain the buffer content and assign it to $ info
Using file1_fopen('info.txt ', 'w'); // open the info.txt file.
Fwrite ($ file, $ info); // write the information to info.txt
Fclose ($ file); // close the info.txt file
// Or directly use file_put_content('info.txt ', $ info );
?> |
The above method saves the phpinfo information of different users.
Here we can focus on the usage tips of this method, and use this method to conveniently generate static pages!
This method is more reasonable and efficient than file_get_conents.
To write the content of phpinfo () to a file, you can do the following:
| The Code is as follows: |
Copy code |
|
Ob_start ();
$ Phpinfo = phpinfo ();
// Write a file
Ob_end_flush ();
Or there is another purpose:
Ob_start (); // open the buffer
Echo "Hellon"; // output
Header ("location: index. php"); // redirects the browser to index. php.
Ob_end_flush (); // output all content to the browser |
Header () will send a file header to the browser, but if there is any output before header () (including empty output, such as space, press enter, and line feed), an error will be reported. However, if the output is between ob_start () and ob_end_flush (), no problem occurs. Because the buffer is opened before the output, the characters after echo will not be output to the browser, but will be retained on the server, knowing that flush will be used for output, so header () will be executed normally.
Of course, ob_start () can also have a parameter, which is a callback function. Example:
| The Code is as follows: |
Copy code |
|
<? Php
Function callback ($ buffer)
{
// Replace all the apples with oranges
Return (str_replace ("apples", "oranges", $ buffer ));
}
Ob_start ("callback ");
? >
<Html>
<Body>
<P> It's like comparing apples to oranges. </P>
</Body>
</Html>
<? Php
Ob_end_flush ();
? >
The above program will output:
<Html>
<Body>
<P> It's like comparing oranges to oranges. </p>
</Body>
</Html> |
For more information, go to the Manual on the official website.