Drupal Coding Standards

Drupal Coding Standards

Indenting and Whitespace

  • Use an indent of 2 spaces, with no tabs
  • Lines should have no trailing whitespace at the end
  • All text files should end in a single newline (n)


  • All binary operators (operators that come between two values), such as +, -, =, !=, ==, >, etc. should have a space before and after the operator, for readability. For example, an assignment should be formatted as $foo = $bar; rather than $foo=$bar;.
  • Unary operators (operators that operate on only one value), such as ++, should not have a space between the operator and the variable or number they are operating on


  • Put a space between the (type) and the $variable in a cast: (int) $mynumber

Control Structures

Control structures include if, for, while, switch, etc. Control statements should have one space between the control keyword and opening parenthesis, to distinguish them from function calls. Always use curly braces even in situations where they are technically optional. Having them increases readability and decreases the likelihood of logic errors being introduced when new lines are added.

Function Calls

Functions should be called with no spaces between the function name, the opening parenthesis, and the first parameter; spaces between commas and each parameter, and no space between the last parameter, the closing parenthesis, and the semicolon $var = foo($bar, $baz, $quux);

Function Declarations

Arguments with default values go at the end of the argument list. Always attempt to return a meaningful value from a function if one is appropriate. function funstuff_system($field) { $system[“description”] = t(“This module inserts funny text into posts randomly.”); return $system[$field]; }

Class Constructor Calls

When calling class constructors with no arguments, always include parentheses:

  • $foo = new MyClassName();
  • $foo = new MyClassName($arg1, $arg2);


Arrays should be formatted with a space separating each element (after the comma), and spaces around the => key association operator, if applicable: $some_array = array(‘hello’, ‘world’, ‘foo’ => ‘bar’);


Drupal does not have a hard standard for the use of single quotes vs. double quotes. Where possible, keep consistency within each module, and respect the personal style of other developers.

String Concatenations

Always use a space between the dot and the concatenated parts to improve readability.


  $string = 'Foo' . $bar;
$string = $bar . 'foo';
$string = bar() . 'foo';
$string = 'foo' . 'bar';

PHP Code Tags

Always use <?php ?> to delimit PHP code, not the shorthand, <? ?>. This is required for Drupal compliance and is also the most portable way to include PHP code on differing operating systems and set-ups.


The PHP language requires semicolons at the end of most lines, but allows them to be omitted at the end of code blocks. Drupal coding standards require them, even at the end of code blocks. In particular, for one-line PHP blocks:

<?php print $tax; ?> -- YES
<?php print $tax ?> -- NO