PHP coding standards

coding-stylePHP

I've been looking for some guidelines on how to layout PHP code. I've found some good references, such as the following:

http://www.dagbladet.no/development/phpcodingstandard/

and this question on SO.

However, none of that quite gets to what I'm specifically wondering about, which is the integration of HTML and PHP. For example:

  1. Is it OK to have a PHP file that starts out with HTML tags and only has PHP inserted where needed? Or should you just have one section of PHP code that contains everything?
  2. If you have a chunk of PHP code in the middle of which is a set of echo's that just output a fixed bit of HTML, is it better to break out of PHP and just put in the HTML directly?
  3. Should functions all be defined in dedicated PHP files, or is it OK to define a bunch of functions at the top of a file and call them later on in that same file?

There are probably other questions I'd like to ask, but really I'm looking for someone to point me at some kind of resource online that offers guidance on the general idea of how HTML and PHP should be combined together.

Best Answer

Combining programming code and output data (including HTML) is IMHO a very bad idea. Most of the PHP gurus I know use a template engine such as Smarty to help keep the two things separate.