PHP Method for Generating html Pages Using ob_start

  • 2021-07-26 07:16:55
  • OfStack

This article illustrates how PHP uses ob_start to generate html pages. Share it for your reference. Specific methods are analyzed as follows:

ob_start ([string output_callback])-Open output buffer

All output information is not sent directly to the browser, but is stored in the output buffer, and an optional callback function is used to process the output information.

ob_end_flush-Ends (sends) the contents of the output buffer and closes the output buffer

The output control function gives you free control over the output of the data in the script, which is useful if you want to output before header.
 

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

  
Personal use of ob is mostly in the generation of static html, when a page will not be refreshed, when other users browse this page again, the program will not call php and related database tutorials. At this time, it is a good practice to use ob to generate html.
<?php
   ob_start();
   if(@readfile($tem_path)){     // Writes the contents in the specified path to the cache. If there is no return false( Is someone you want to convert into html Adj. php Documents )
          $content= ob_get_contents(); // Get the contents of the cache
          $fp = fopen("1.html", "w"); // Create 1 File, and open, ready to write
          fwrite($fp, $content); // Put php The contents of the page are all written to 1.html
   }
     fclose($fp);
   ob_clean();
?>

I hope this article is helpful to everyone's PHP programming.


Related articles: