Sing Xing explains why PHP needs a template

Source: Internet
Author: User
Xin Xing explained why PHP needs a template. Someone recently asked me: why does PHP need a template? I write the entire website by myself. from the front-end to the back-end, this is the case. Do I not need a template to write a website by myself? I did not think of a proper way to answer the question at the time, so I just gave a few reasons. now I think about the reasons below.

The first point is the separation of PHP code and HTML code. it is not something that several people do, just as the front-end HTML and CSS are usually written by one person, so why don't we fix the layout in HTML directly? The reason is the chaos. we write CSS code and HTML code together. you can look at webpages on websites such as Sina. I actually did a look at the lines 6606 and more than six thousand, modify the style and try to modify the style. if you want to change the style, you have to find it for half a day. that's right. so, we are pursuing separating HTML and CSS to facilitate search and modification.

I have also written a lot of HTML and PHP mixed web pages, mainly used for teaching examples, but most of them are not long, that is, about 200 or 300 lines, at that time, I felt a little difficult to modify the PHP code. if I wanted to modify the PHP code, I had to find it in line 2. the key was that the three hundred lines were not all PHP code, but mixed-edited, so we have to differentiate them. this is very troublesome. if we write them separately, we write over one hundred lines of PHP code, and then write about two hundred lines of HTML code, whether we modify our PHP or HTML, it is very convenient. why, because there are few codes, and this is more advantageous for the logic of PHP code, it makes PHP code clearer and easier to debug errors.

The second point is the cache, which is caused by the fact that new users often do not know the importance of the cache. the cache is very important. I remember a master said that if PHP has a built-in cache, its performance can be improved by five hundred times. Imagine that there are more than six thousand lines of static pages on the home page of Sina website. If PHP is used, it needs to retrieve data from the database first, then complete the relevant logic and then hand it over to Apache or nigix for processing. the overhead of the entire process is very high. Therefore, we convert the webpage static into a fixed HTML page. in this way, the server directly sends static pages, improving the efficiency by at least 10 times. This static method requires the template technology. no template does not support caching. many templates can interpret PHP native code because caching is important, instead of writing the template symbol.

Some people may say that my website is a small site. Do I still need to cache it? The answer is that you don't need to cache it, but the cache will never drag you down (although it will occupy a small number of hard disks). Therefore, supporting page caching will not drag your website down. of course, other caches are hard to say. in this regard, using the template cache will not play a negative role.

The third point is that it is better to use templates when multiple people work on development, because most of the template technologies have a PHP webpage variable corresponding to the variable technology of the template webpage, everyone's naming has their own habits. This ensures that many people work better together and will not appear. he must know what you want to do before doing it, in this way, we can better coordinate the work of multiple people and make the work more efficient. So does one need to use a template? The answer is to check your workload. if you don't feel the trouble of modifying PHP code on the webpage, you don't need to. if you are tired of modifying PHP code on the webpage, then I am afraid the template technology is required.

The fourth point is Universality. how can this be said? if the reader is engaged in PHP development from the very beginning, he will not feel it. if the reader has developed Python or Java before, then we will feel the importance of it, because Java and PHP and HTML are not as good as PHP. Therefore, by using templates, developers in other languages can use PHP for development quickly. This ensures that users who have experience in other languages do not want to transfer to the PHP language, because I used Java before, I know this ..

If you have any comments, you can leave a message and I will reply carefully.

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.