
Coding standards (2099 Views)
Single and double quotes
Use single and double quotes when appropriate. If you're not evaluating anything in the string, use single quotes. You should almost never have to escape HTML quotes in a string, because you can just alternate your quoting style, like so:
echo "<a href='$link' title='$linktitle'>$linkname</a>";
echo '<a href="/static/link" title="Yeah yeah!">Link name</a>';
The only exception to this is JavaScript, which sometimes requires double or single quotes. Text that goes into attributes should be run through Texturize so that single or double quotes do not end the attribute value and invalidate the XHTML.
Indentation
Your indentation should always reflect logical structure. Use real tabs and not spaces, as this allows the most flexibility across clients.
Brace Style
Brace styles, when used correctly, makes for easier reading. An example:
if ( (condition1) || (condition2) ) {
action1;
}
elseif ( (condition3) && (condition4) ) {
action2;
}
else {
defaultaction;
} // end blah
Furthermore if you have a really long block, consider if it can be broken into two or more shorter blocks or function. If you consider such a long block unavoidable, please put a short comment at the end so people can tell at glance what that ending brace ends -- typically this is appropriate for a logic block, longer than about 35 rows, but any code that's not intuitively obvious can be commented.
include_once vs. require_once
Learn the difference between include_once (http://us3.php.net/manual/en/function.include-once.php) and require_once (http://us3.php.net/manual/en/function.require-once.php), and use each as appropriate. To quote the php manual page on include() (http://us3.php.net/manual/en/function.include.php): "The two constructs are identical in every way except how they handle failure. include() produces a Warning while require() results in a Fatal Error." Fatal errors stop script execution.
Regular expressions
Perl compatible regular expressions (PCRE, preg_ functions) should be used in preference to their POSIX counterparts.
No Shorthand PHP
Never use shorthand PHP start tags (<? ... ?>

. Always use full PHP tags (<?php ... ?>

.
Space Usage
Always put spaces after commas and on both sides of logical and assignment operators like "x == 23", "foo && bar", "array(1, 2, 3)", , as well as on both sides of the opening and closing parenthesis of if, elseif, foreach, for and switch blocks (e.g. foreach ( $foo as $bar ) { ...)
Formatting SQL statements
When formatting SQL statements you may break it into several lines and indent if it is sufficiently complex to warrant it. Most statements work well as one line though. Always capitalize the SQL parts of the statement like UPDATE or WHERE.
Variables, functions, and operators
If you don't use a variable more than once, don't create it.
Ternary operators are fine, but always have them test if the statement is true, not false. Otherwise it just gets confusing.
// GOOD example:
// (if statement is true) ? (do this) : (if false, do this);
$musictype = ('jazz' == $music) ? 'cool' : 'blah';
Another important point in the above example, when doing logical comparisons always put the variable on the right side, like above. If you forget an equal sign it'll throw a parse error instead of just evaluating true and executing the statement. It really takes no extra time to do, so if this saves one bug it's worth it.
// GOOD
if ( 'foo' == $bar )
// BAD
if ( $bar == 'foo' )