Home >Backend Development >PHP Tutorial >How to use ob_start to generate html page in PHP_PHP tutorial

How to use ob_start to generate html page in PHP_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:14:53816browse

How PHP uses ob_start to generate html pages

ob_start ([string output_callback]) - Open the output buffer

All output information is no longer sent directly to the browser, but is saved in the output buffer. An optional callback function is used to process the output result information.

ob_end_flush - End (send) the contents of the output buffer, close the output buffer

Using the output control function allows us to freely control the output of data in the script, which is useful when we want to output before the header.

The code is as follows:

ob_start(); //Open buffer
echo "output n"; //output
header("Header information");
ob_end_flush();//Output all content to the browser
?>



Most of my personal use of OB is when generating static HTML. When a page will not be refreshed, and when other users browse this page again, the program will no longer call PHP and related database tutorials. At this time, it is a good practice to use ob to generate html.

The code is as follows:

ob_start();
if(@readfile($tem_path)){ //Write the content in the specified path to the cache. Return false if it does not exist (it is a php file you want to convert to html)
​​​​$content= ob_get_contents(); //Get the content in the cache
$fp = fopen("1.html", "w"); //Create a file and open it for writing
fwrite($fp, $content); //Write all the contents of the php page into 1.html
}
​fclose($fp);
ob_clean();
?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/907709.htmlTechArticlePHP uses ob_start to generate html page method ob_start ([string output_callback]) - Open all output information in the output buffer Instead of sending it directly to the browser, it saves it in the output buffer...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn