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.
public function __construct() {} rather than the PHP 4 style class name.
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.
+, =, !=, -, ==, >, 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.++, --) 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.
else if, rather than elseif.
if (condition1 || condition2) {
action1;
} else if (condition3 && condition4) {
action2;
} else {
defaultaction;
}
switch (condition) {
case 1:
action1;
break;
case 2:
action2;
break;
default:
defaultaction;
}
for ($i = 0; $i < condition; $i++) {
action1;
}
: may be used in place of brackets.<?php if (!empty($item)): ?> <p><?php print $item; ?></p> <?php endif; ?><?php foreach ($items as $item): ?>
<p><?php print $item; ?></p>
<?php endforeach; ?>
$var = foo($bar, $apple, $peach);
$arr = array('hey', 'jude', 'git' => 'commit');$arr = array( 'one' => $one, 'two' => $two, 'three' => $three, 'four' => $four, 'count' => 26 );
...more to come.