I have a file with php and html codes mixed, like this:
echo "<tr class='somthing'>";
What is the best way to keep my php files with only php code?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
There is some contention about this, so I will simply say that whatever you choose, stick with it.
Generally, you can use a templating system (e.g., Smarty) to keep your files formally clean of PHP code, but this has some drawbacks in the speed department.
Alternatively, you can use minimal code by breaking out the display related portions and using small PHP snippets therein, e.g.
Of course this doesn’t completely separate your code from your HTML, but that’s nearly impossible to do for anything nontrivial; in my opinion, Smarty templates are just different code, not the absence of code. This approach is advocated by some frameworks such as codeigniter and Zend Framework (thought you can use them with Smarty as well, it is not the default).
One important thing to remember is that your display logic is just that: logic. Whatever method you choose, you will never be 100% free of code (or code-like markings which are interpreted by other code/applications) in the display until you use static-only displays.