Parse the ob_start of function in PHP

  • 2020-06-19 09:55:05
  • OfStack

ob_start () function is used to open the buffer, such as header before if there is a () function outputs, including carriage return/space / / a new line will be "Header had all ready send by" mistakes, then you can use first ob_start () open the buffer PHP code blocks of data and echo () output will not immediately output into the buffer. Open the buffer role, of course, a lot of, as long as you use your imagination. You can summarize the following four points:

1. Before header()
ob_start (); // Open buffer
Hellon echo/"/"; / / output
header (" location: index. php "); // Redirect the browser to index.php
ob_end_flush (); Output all content to the browser
? >

2. The phpinfo() function retrieves both client and server information, but a buffer to hold client information is the best option.
ob_start (); // Open buffer
phpinfo (); // Use the phpinfo function
$info = ob_get_contents (); // Gets the contents of the buffer and assigns a value to $info
$file = fopen (/ 'info. txt/'/' w/'); // Open file info.txt
fwrite ($file, $info); // Write the information to ES64en.txt
fclose ($file); // Close the file info.txt
? >

3. Static page technology
ob_start (); // Open buffer
? >
Full output of the php page
$content = ob_get_contents (); Get all output from php page
$fp = fopen (" output00001 html ", "w"); // Create 1 file, open and ready to write
fwrite ($fp, $content); // Write php 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;
}


Related articles: