Sinsing explaining why PHP needs templates

Source: Internet
Author: User
A person recently asked me: Why does PHP need a template? The whole site was written by me, from the front to the back end, all this, I am a person to write the site is not required template it? I was really asked to live, did not think of a very appropriate way to answer it, so casually said a few reasons, now carefully think about, there are a few reasons.

The 1th is the separation of PHP code and HTML code, it is not a few people do things, like the front-end HTML and CSS is usually a person to write, then why not directly in the HTML layout? The reason is chaos, we put the CSS code and HTML code together, we can look at such as Sina, such as the Web page, I really looked at a moment ago, 6606 lines, more than 6,000 lines, modify the style in the inside, want to change something, just look for a half a day, yes, so, We seek to separate the HTML and CSS, is to find the convenience of modification.

I have also written a lot of HTML and PHP mixed pages, mainly used for teaching examples, but mostly not long, but also about two hundred or three hundred rows of the look, that time I modified to feel a little laborious, if you want to modify the PHP code, must be found in 300 lines, the key is that the 300 lines are not all PHP code, but mixed, So we have to distinguish, this is very troublesome, if Vaughn separate write, we write more than 100 lines of PHP code, and then write more than 200 lines of HTML code, whether we go to modify their own PHP or to modify the HTML, is very convenient, why, because the code is small ah, and this for the logic of PHP code is more beneficial, It allows us to write PHP code ideas clearer and easier to debug errors.

The 2nd is the cache, which is also a novice often do not know the importance of caching caused by the cache is very important. I remember a master who said that if PHP had built-in cache, it would have improved its performance by 500 times times. Imagine, Sina website so much content, the light static page has more than 6,000 lines, if let PHP to do, it first need to go to the database to take out the data, and then to complete the relevant logic processing, and then to Apache or Nigix to deal with, the whole process of overhead is very big, so, We static the page into a fixed HTML page, so that the server directly send static pages, the efficiency is at least 10 times times higher. This static, you need to use the template technology, no template does not support caching, many templates can interpret PHP native code because of this, because the cache is important, rather than writing the template of what the symbol is.

Someone might say, "My site is a small station, do I need a cache?" The answer is you can not cache, but the cache will never drag you, (although it will take up a small amount of hard disk), so support page cache does not drag your site, of course, the other cache is not good to say, from this point of the use of the template's cache will not play a negative role.

The 3rd is a multi-person division of labor Development, the use of templates better, because most of the template technology has a PHP Web page variables and template Web page variable technology correspondence, many people develop the time, each person's name has its own habits, which can ensure that many people work better cohesion, Doesn't show up. He must know how you are going to do it, so you can work better with more people and make it more productive. Does a person need to use a template? The answer is to look at your workload size, if you do not feel trouble in the Web page to modify the PHP code, you can not, if you feel very tired to modify the PHP code in the Web page, then I am afraid to use template technology.

4th is the universality, this is how to say, if the reader is the beginning of the development of PHP, then certainly do not feel, if the reader has developed Python or Java, it will feel the importance of it, because Java and PHP and HTML are not as good as PHP, so, By using templates, we can make it possible for people working in other languages to develop with PHP in a very fast way, to make sure that there is no other language experience that doesn't want to go into the PHP language because I was in Java before, and I know that.

If the reader has any views, can give me a message, I will seriously reply.

  • 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.