Page 1 of 1
best practices of PHP/HTML web UI application coding.
Posted: Sat Mar 20, 2010 2:27 pm
by sureshgl
Dear PHP Master,
I am new to the php based web application development. I have little bit of experience in writing perl based cgi applications.
The question is what is the "best practice in coding php applications".
Most of the php examples include php logic coding and html design tags as interleaved, actually this is creating a lot of confusion in following the php code and consolidating the application logic.
For example take Bugzilla, perl/cgi based coding, has followed a design/coding practice, where templates are separate from the logic . Similarly phpBugTracker also tried to follow the same , but not very accurately.
Please let me know the best practice in PHP & HTML based application.
1. Should we interleave the PHP code and HTML tags.
2. Should we keep them separate.
Also please mention the applications following the best practices.
Thanks & Regards,
GL.
Re: best practices of PHP/HTML web UI application coding.
Posted: Sat Mar 20, 2010 2:33 pm
by Eran
One of the best practices of this forum is to not post the same question three times in a row.
Re: best practices of PHP/HTML web UI application coding.
Posted: Sat Mar 20, 2010 2:37 pm
by sureshgl
Sorry. Please consider that as newbie mistake.
Re: best practices of PHP/HTML web UI application coding.
Posted: Sat Mar 20, 2010 4:42 pm
by Christopher
sureshgl wrote:1. Should we interleave the PHP code and HTML tags.
Yes in templates. Unlike other languages, it is very effective to mix HTML and PHP.
sureshgl wrote:2. Should we keep them separate.
Yes, you should keep your templates (HTML & PHP) separated from your application files (PHP only).
Re: best practices of PHP/HTML web UI application coding.
Posted: Sat Mar 20, 2010 11:01 pm
by josh
Christopher wrote:sureshgl wrote:1. Should we interleave the PHP code and HTML tags.
Yes in templates. Unlike other languages, it is very effective to mix HTML and PHP.
I would say you should draw the line somewhere. If you are "doing work" or "computations" in the same file as HTML code, I would say that is bad. At most you should be switching (if statement) based on a value of a variable, or outputting a variable, or looping over an array... No more "work" then that.
Re: best practices of PHP/HTML web UI application coding.
Posted: Sun Mar 21, 2010 1:47 am
by Christopher
josh wrote:I would say you should draw the line somewhere. If you are "doing work" or "computations" in the same file as HTML code, I would say that is bad. At most you should be switching (if statement) based on a value of a variable, or outputting a variable, or looping over an array... No more "work" then that.
I think as long as the code in the template is doing presentation stuff related to converting the template into the output, then I am fine with it. I would put no artificial restrictions like "doing work" or "computations" because they are undefinable.
My point was not getting that specific anyway. I was trying to convey the fact that PHP is both a templating language and an application language -- unlike most other languages. The fact that PHP is designed to be embedded in HTML is fairly unique. But we also all create PHP only files that contain more traditional application code.
Re: best practices of PHP/HTML web UI application coding.
Posted: Sun Mar 21, 2010 4:00 am
by josh
Christopher wrote:My point was not getting that specific anyway.
Mine neither. I guess my choice to use "undefinable terms" can be assumed to imply that asking the "best practice" is also undefinable, it is subjective to some degree.
You could define "doing work" as business logic. You "defined" what should go in the template as "presentation logic". So yes there is a line that should be drawn between presentation & business logic (hence the distinct terms), but every different person and/or project might entail drawing that line in different places (for example if your project involves legacy code, you might relax your "rules" or allow that line to be blurred)
For example in your template you might only want to render a certain section based on the value of some variable/field. Doing SQL queries can be considered "the work" involved in figuring it out. That should be done in the controller (if doing 2-tier) or in the model (if doing N-tier). Once that calculation is done, (SQL query.. whatever), it should be stored in a variable, and that variable passed to the view.
Any code in the view should read like plain English. A person with limited programming knowledge should be able to describe what it does without asking you questions.
when I say it "should be done this way" I mean if you want to separate it. If you make a project level decision not to separate it, thats not necessarily "bad", depending on circumstances (example working on legacy code)