Always use <?php
to delimit PHP code. Do not use the shorthand version, <?
. PHP short tags are not enabled by default on new installations and are deprecated.
- Class constructors should use
public function __construct() {}
rather than the PHP 4 style class name. - Use class destructors where appropriate.
- Explicitly declare visibility of member methods and variables (public, private, protected).
-
Do NOT use closing tags for files containing only PHP code.
The
?>
at the end of code files is purposely omitted. This includes for module and include files. ... Removing it eliminates the possibility for unwanted whitespace at the end of files which can cause "header already sent" errors, XHTML/XML validation issues, and other problems.
- Use indent of 2 spaces, no tabs.
- Leave no trailing whitespace at the end of lines.
- Files should be formatted with Unix line endings.
- Binary operators (
+, =, !=, -, ==, >,
etc.) should include a space before and after the operator, for readability. For instance, an assignment would be formatted as$lorem = $ipsum
rather than$lorem=$ipsum
. - Unary operators (
++, --
) 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 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.
- Wrap ternary logic in parens.
- Don't use
else if
, but ratherelseif
.
if (condition1 || condition2) { action1; } elseif (condition3 && condition4) { action2; } else { defaultaction; }
switch (condition) { case 1: action1; break;case 2: action2; break;
default: defaultaction; }
for ($i = 0; $i < condition; $i++) { action1; }
- In templates, a
:
may be used in place of brackets. - Do no use a space between the closing paren and the colon.
- HTML inside the control structure should be indented.
<?php if (!empty($item)): ?> <p><?php print $item; ?></p> <?php endif; ?><?php foreach ($items as $item): ?> <p><?php print $item; ?></p> <?php endforeach; ?>
- Functions should be called with no spaces between the function name and the opening parenthesis.
- No space between the opening paren and the first parameter.
- Spaces between commas and each parameter.
- No space between the last parameter, the closing parenthesis, and the semicolon
-
Here's an example:
$var = foo($bar, $apple, $peach);
- A space should separate each element.
- Add spaces around the => key association operator.
- Example:
$arr = array('hey', 'jude', 'git' => 'commit');
- If the array spans more than 80 characters, each element should be broken up on its own line.
- Multi-line declarations should have elements indented.
$arr = array( 'one' => $one, 'two' => $two, 'three' => $three, 'four' => $four, 'count' => 26 );
...more to come.
Since the style guide is similar to Drupal's, we can steal some configuration from this "drupal-mode" for our php-mode (this goes in .emacs, naturally):
(add-hook 'php-mode-hook (lambda () (setq c-basic-offset 2) (setq indent-tabs-mode nil) (setq fill-column 78) (setq show-trailing-whitespace t) (add-hook 'before-save-hook 'delete-trailing-whitespace) (c-set-offset 'case-label '+) (c-set-offset 'arglist-close 0) (c-set-offset 'arglist-intro '+) ; for FAPI arrays and DBTNG (c-set-offset 'arglist-cont-nonempty 'c-lineup-math))) ; for DBTNG fields and values