Parsing the usage of the ob_start () function in PHP _ PHP Tutorial

Source: Internet
Author: User
Parse the usage of the ob_start () function in PHP. The ob_start () function is used to open the buffer. for example, if there is output before the header () function, Headerhadallreadysendby will occur if there is a line feed including a carriage return space. in this case, you can use ob _ ob_start () first () the function is used to open the buffer. for example, if there is output before the header () function, including carriage return, space, and line feed, there will be an error of "Header had all ready send, 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;
}

The http://www.bkjia.com/PHPjc/327817.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/327817.htmlTechArticleob_start () function is used to open the buffer, for example, if there is output before the header () function, including carriage return/space/line feed/will have "Header had all ready send by" error, in this case, you can use ob _...

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.