PHP dynamic variable static principle _php Foundation

Source: Internet
Author: User
Tags php template
The way to output static pages in PHP, as I know, has 2 kinds, one is using template technology, the other is using OB series functions. Both methods look the same, but in fact, they are different.

The first: the use of templates。 The current PHP template can be said to be a lot of, there is a powerful smarty, there are simple and easy-to-use smarttemplate and so on. Each of these templates has a function that gets the output. The way we generate static pages is to take advantage of this function. The advantage of this method is that the code is clearer and more readable.

Here I use Smarty as an example to show how to generate a static page
Copy Code code as follows:
<?php
Require (' smarty/smarty.class.php ');
$t = new Smarty;
$t->assign ("title", "Hello world!");
$content = $t->fetch ("templates/index.htm");
The fetch () here is the function to get the output, and now the $content variable is the content to be displayed.
$fp = fopen ("archives/2005/05/19/0001.html", "w");
Fwrite ($fp, $content);
Fclose ($FP);
?>

The second method: Use functions from the OB series. The functions used here are mainly Ob_start (), Ob_end_flush (), Ob_get_content (), where Ob_start () is to open the browser buffer, when the buffer is turned on, all non-file header information from the PHP program will not be sent, Instead, save it in the internal buffer until you use the Ob_end_flush (). And the most important function here is ob_get_contents (), the function of which is to get the contents of the buffer, which is equivalent to the fetch (). Code:
Copy Code code as follows:
<?php
Ob_start ();
echo "Hello world!";
$content = ob_get_contents ()//Get all content of PHP page output
$fp = fopen ("archives/2005/05/19/0001.html", "w");
Fwrite ($fp, $content);
Fclose ($FP);
?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.