简体   繁体   中英

What are the advantages of separating php and html?

分离PHP和HTML内容有哪些优缺点?

The advantages mostly pertain to code readability, which in large applications plays a huge part in the maintenance of the application.

The disadvantages are that it sometimes makes advanced functionality hard to execute. Most of the time, it can be done and still keep the two separate, but it's often much simpler and easier to just insert php snippets into html code or vice-versa if its just a small amount of code.

It's a trade off between ease of execution in certain cases and readability. For the most part, I would recommend trying to keep them separate.

HTML is just for the representation of your results / forms etc (your view), see MVC pattern for more info.

If you separate it from your business logic, you'll be able to generate other views easily (eg JSON for Javascript).

If you're using a templating engine as well, your HTML/CSS gurus can work on the look and feel independently as well.

Separating program logic (the PHP part) from presentation (the HTML part) is beneficial for several reasons:

  • It allows you to change the presentation without affecting the program's inner workings, that is, you're not altering what it does by changing the layout
  • It allows for independent testing of both parts: you can execute parts of the program logic from a test script and inspect the results, which means you can automate a large portion of your testing
  • Maintenance becomes much easier, because you have less code to look through when looking for errors
  • For larger teams, the application can be structured in a way that allows designers (that is, people with little understanding of programming) to alter the HTML part independently without much risk of breaking the program logic
  • It enables you to focus on one problem at a time. You don't want to burden your mind with HTML details while debugging algorithms, and vv.
  • Code reuse: If your presentation layer delegates its calls to the logic layer, instead of doing it itself, chances are you'll be reusing that logic elsewhere; having it in the logic layer means you can just call it instead of copy-pasting all over the place (which in turn leads to maintenance nightmares)

There is a great advantage to separating the two because you can edit html code without breaking the PHP code. Smarty is a good template engine to learn.

The main reason is your code will be ugly if you merge those php ( business logic ) with the html ( presentation ) together, which in turn become hard to read and become hard to maintain . It won't be a problem if your web application is a simple one. But if it's a enterprise scale project, maintaining this merged code will be a nightmare for anyone.

Beside, the kind of programming you do for each part and sometimes the language you do it in are different. Separating the two allows one to use the best tools for that particular part.

source: http://www.paragoncorporation.com/ArticleDetail.aspx?ArticleID=21

如果你想分割php代码和html,使用任何php框架,如codeigniter,cakephp,zend,yii等,主要优点是,如果你要改变网站设计而不是你的功能,那时它将非常有用,我们也可以以可重用的方式开发代码。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM