Parse the usage of the ob_start () function in PHP

Source: Internet
Author: User

The ob_start () function is used to open the buffer. For example, if there is output before the header () function, including carriage return, space, line feed, and "Header had all ready send by" errors, in this case, you can use ob_start () to open the data block of the PHP code in the buffer zone and echo () to output the data in the buffer zone rather than immediately. of course, opening a buffer has a lot to do, as long as you can make full use of your imagination. the following four points can be summarized:

1. used before header ()
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
?>

2. The phpinfo () function can obtain information on the client and server, but it is best to save the client information using the buffer method.
Ob_start (); // open the buffer
Phpinfo (); // use the phpinfo Function
$ Info = ob_get_contents (); // obtain the buffer content and assign it to $ info
$ File = 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
?>

3. Static Page Technology
Ob_start (); // open the buffer
?>
All php page output
$ Content = ob_get_contents (); // Retrieves all the content output on the php page.
$ Fp = fopen ("output00001.html", "w"); // create a file, open it, and write
Fwrite ($ fp, $ content); // write the content of the PHP page into output00001.html, and then ......
Fclose ($ fp );
?>

4. Output code
Function run_code ($ code ){
If ($ code ){
Ob_start ();
Eval ($ code );
$ Contents = ob_get_contents ();
Ob_end_clean ();
} Else {
Echo "error! No output ";
Exit ();
}
Return $ contents;
}

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.