Search code examples
phpcoding-style

PHP coding standards


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.


Solution

  • There's really not a single, common standard for these things. Most languages are more restrictive than PHP in this sense.

    In the later years, a lot of so-called frameworks have emerged, and amongst other things they define a set of rules for everything from naming over where to place files and to which style your code should follow. There are several frameworks around, so you can't really pick one and call it the standard. However, most frameworks have a subset of commonality. For example, most follows some variant of the PEAR Coding Standards.