Parsing the use of Ob_start () functions in PHP _php tips

Source: Internet
Author: User
Tags file info phpinfo

Ob_start () function is used to open a buffer, such as the header () function, if there is output before, including carriage return/space/line break/will have "Header had all ready send" error, you can first use Ob_start () The data Block and Echo () output of the open buffer PHP code will enter the buffer without immediate output. Of course, opening the buffer has a lot of effect, just play your imagination. You can summarize the following four points:

1. Before header ()
Ob_start (); Open buffer
echo/"hellon/"; Output
Header ("location:index.php"); redirect browser to index.php
Ob_end_flush ()//output all content to browser
?>

The 2.phpinfo () function obtains information on both the client and server side, but the best option is to save the client information in a buffer.
Ob_start (); Open buffer
Phpinfo (); Using the Phpinfo function
$info =ob_get_contents (); Gets the contents of the buffer area and assigns it to $info.
$file =fopen (/' info.txt/',/' w/'); Open File Info.txt
Fwrite ($file, $info); Write information to Info.txt
Fclose ($file); Close File Info.txt
?>

3. Static page Technology
Ob_start ()//Open buffer
?>
All output of PHP page
$content = ob_get_contents ()//Get all content of PHP page output
$fp = fopen ("output00001.html", "w"); Create a file and open it, ready to write
Fwrite ($fp, $content); Write the contents of the PHP page to 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.