Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 905
Ben Avatar asked Nov 20 '08 22:11

Ben


People also ask

What are coding standards?

What are Coding Standards? Think of coding standards as a set of rules, techniques, and best practices to create cleaner, more readable, more efficient code with minimal errors. They offer a uniform format by which software engineers can use to build sophisticated and highly functional code.

Is PHP standardized?

The PHP Standard Recommendation (PSR) is a PHP specification published by the PHP Framework Interoperability Group (PHP-FIG). It serves the standardization of programming concepts in PHP. The aim is to enable interoperability of components. The PHP-FIG is formed by several PHP frameworks founders.

What is your effective way to follow the coding standard in PHP?

There are few guidelines which can be followed while coding in PHP. Indenting and Line Length − Use an indent of 4 spaces and don't use any tab because different computers use different setting for tab. It is recommended to keep lines at approximately 75-85 characters long for better code readability.


1 Answers

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.

like image 158
Sherm Pendley Avatar answered Sep 28 '22 07:09

Sherm Pendley