Home  >  Article  >  Backend Development  >  PHP uses ob_start to generate html pages, phpob_start_PHP tutorial

PHP uses ob_start to generate html pages, phpob_start_PHP tutorial

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

PHP uses ob_start to generate html pages, phpob_start

The example in this article describes how PHP uses ob_start to generate html pages. Share it with everyone for your reference. The specific method analysis is as follows:

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.

Copy code 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.
Copy code 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();
?>

I hope this article will be helpful to everyone’s PHP programming design.

How to generate HTML page in php requires detailed generation process code

It’s very simple, use ob_start to suppress it, and then save it to an html page. I wrote a small demonstration
Add parameters at the end when accessing? id=xxx xxx is any number, you can replace the output inside Your dynamic page is enough, there are explanations in the code

ob_start();

/*The following content is your original dynamic page*/
$id=isset($_GET["id"])?$_GET["id"]:'';
if ($id!=''){
echo "Original page parameters using parameters Generated$id.html";
}
/*End* for ".$id."
/

$info=ob_get_contents();
$file=fopen($id.'.html','w');
fwrite($file,$info);
fclose($file);
?>

After PHP generates HTML, how to link to the page

The following three methods can be used:
1. Use hyperlinks to connect text
You can also add " ?id=idvalue&cate=catevalue" and so on to pass the necessary parameters to test.php. In test.php, you can use $_GET[id] and $_GET[cate] (or $_REQUEST[id] and $_REQUEST[cate]) Get the value of the parameter.
2. Use the form to submit

Form content

The method here can be GET (test.php, and the obtained parameters are as follows: 1 ; It can also be POST, and the acquisition parameters are similar to 1, but change GET to POST, REQUEST is common)
3. Use ajax technology.
In addition, you can also use the frame technology in html.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/907839.htmlTechArticleHow PHP uses ob_start to generate html pages, phpob_start This article describes how PHP uses ob_start to generate html pages. Share it with everyone for your reference. The specific method analysis is as follows:...
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