Parsing the use of the Ob_start () function in PHP
This article is a detailed analysis of the usage of the Ob_start () function in PHP, which requires a friend's reference
The Ob_start () function is used to open the buffer, such as the header () function, if there is output, including carriage return/space/line break/There will be "Header had all ready to send by" error, you can first use Ob_start () The data Block and Echo () output that open the buffer PHP code will go into the buffer without immediately outputting it. Of course, opening the buffer is a lot of work, 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 the browser to index.php
Ob_end_flush ();//output all content to the browser
?>
The 2.phpinfo () function obtains information on both the client and server side, but the way to save the client information with buffers is the best choice.
Ob_start (); Open buffer
Phpinfo (); Using the Phpinfo function
$info =ob_get_contents (); Get the contents of the buffer area and assign 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
?>
Full output of PHP page
$content = Ob_get_contents ();//Get all the contents of the 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, 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;
}
Parsing the use of the Ob_start () function in PHP