Php ob_start () function to implement cache output of the current page content

Source: Internet
Author: User
Tags phpinfo


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.

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.