What are the advantages and disadvanta开发者_运维百科ges of separating PHP and HTML content?
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 (e.g. 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
if u want to divide the php code and html, use any php frameworks, such as codeigniter, cakephp, zend, yii, etc., the main advantage is, if ur going to change the site design not your functionality, at that time it will be very useful and also we can develop the code in reusable manner.
精彩评论