Strangecode coding standards 29 Aug 2005 20:12:49 ====================================================================== Preable ====================================================================== We're following the PEAR coding standards, with minor modifications. This is essential reading: http://pear.php.net/manual/en/standards.php ====================================================================== File naming conventions ====================================================================== script.php Public accessible scripts. Auth_SQL.inc.php One PHP Class to be included. The filename is the type of class, underscore, name. Or if in subdirs, this could be /Auth/SQL.inc.php while the class name remains "Auth_SQL" some_file.ihtml HTML file, which may or may not have some PHP, but will be included by some other PHP file. Never includes sensitive code as these files may be accessed directly in the web root. script.cli.php A command-line executable script, possibly executed with CRON, usually outputs TEXT, not HTML. dbname.mysql Database schema file that goes with the application. main.screen.css CSS file with media: screen/print/all main.print.css ====================================================================== Indenting and wrap ====================================================================== Use an indent of 4 spaces, with no tabs. Code and especially comments should be wrapped <= 80 characters. Exceptions are made in the case where code readability is significantly improved with longer lines. ====================================================================== Control Structures ====================================================================== These include if, for, while, switch, etc. Here is an example if statement, since it is the most complicated of them: if ((condition1) || (condition2)) { action1; } else if ((condition3) && (condition4)) { action2; } else { defaultaction; } Control statements should have one space between the control keyword and opening parenthesis, to distinguish them from function calls. Even in the case of an if with no else clauses, and only a 1 line action, the curly braces should still be used: if (condition) { action; } This improves readability and allows easy extension of the clause. For switch statements: switch (condition) { case 1: action1; break; case 2: action2; break; default: defaultaction; break; } ====================================================================== 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. Here's an example: $var = foo($bar, $baz, $quux); As displayed above, there should be one space on either side of an equals sign used to assign the return value of a function to a variable. In the case of a block of related assignments, more space may be inserted to promote readability: $short = foo($bar); $long_variable = foo($baz); ====================================================================== Function Definitions ====================================================================== Function declaractions follow the "one true brace" convention: function fooFunction($arg1, $arg2 = '') { if (condition) { statement; } return $val; } 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. Recommend using studlyCaps for function names, to distinguish from php internal functions which standard on under_score_space() style names. ====================================================================== Return values ====================================================================== When functions return boolean values, use 'return false;' or 'return true;' as opposed to 'return 0;' or 'return 1;' or 'return(-1);'. ====================================================================== String concatination ====================================================================== Always include a space before and after the concatonation operator '.' which improves readability and improves search-and-replace of adjacent elements. $something = $blah . funky() . ".\".=" . $blab; is better than: $something = $blah.funky().".\".=".$blab; ====================================================================== Quote marks ====================================================================== Use the single quote marks ' to enclose simple strings whenever possible. Double quote marks " require extra parsing and thus slow things down, but are necessary if entities there must be swapped-out such as variables or control characters. $var['singlequote'] = 'singlequote'; $var["doublequote-$i"] = "$vars and \n funny \t %s things need doublequotes"; $var['doublequote-' . $i] = $var . 'you can do this' . "\t %s" . $var2 . 'but it isn\'t any better'; ====================================================================== Printing html ====================================================================== For large or complex blocks of HTML, using the following method is much faster and safer than echo because the php processesor is bypassed and content goes directly to output: