- Alway use <?php ?>, never the shorthands <? ?> or <?= ?>
- Choose understandable names for functions and variables.
- Indent your code with tabs in a logical manner.
- Use curly brackets, even if they are not required
- Code: Select all
//BAD example (for point #2 as well)
if ($t == '')e('undefined', 'file', $fl);
// better:
if ($temp == ''){
error_message('undefined', 'file', $file);
}
- Use single quotes, at least in case when there is nothing to evaluate in the string:
- Code: Select all
//bad:
echo "<a href=\"http://www.cmsimple.org\" title=\"CMSimple\">CMSimple</a>";
//better:
echo '<a href="http://www.cmsimple.org" title="CMSimple">CMSimple</a>';
- Separate parameters and operators with spaces
- Code: Select all
//bad:
function some_function($foo,$bar){
if($foo==bar){
return $foo+$bar;
}
}
//better:
function some_function($foo, $bar){
if($foo == bar){
return $foo + $bar;
}
}
- Code: Select all
//bad:
if(isset($foo) && $foo > 1 && $foo < 45 && $bar == true){
//...
}
//better:
if( isset($foo)
&& $foo > 1
&& $foo < 45
&& $bar == true
){
//...
}
- Do not nest too many things and don't write code lines with more than about 80 characters.
- Code: Select all
//bad:
$foo = explode('§', preg_replace("/(<h[1-".$cf['menu']['levels']."][^>]*>)/i", "§\\1", str_replace('§', '§', rf($pth['file']['content']))));
//better: (not good anyway, but at least better to understand)
$foo = rf($pth['file']['content']);
$foo = str_replace('§', '§' $foo);
$foo = preg_replace('/(<h[1-'.$cf['menu']['levels'].'][^>]*>)/i', '§\\1', $foo);
$foo = explode('§', $foo);
- Function names should start with a lower case letter, the single words should be separated from each other. (But what is better camel cased (myFunction()) or the underscore solution (my_function()?)
- Class definitions should start with an uppercase and have a linebreak before the opening bracket
- Code: Select all
class Toy
{
//...
}
- Indent the assignments in arrays:
- Code: Select all
//bad:
array('drink' => 'coffee', 'do_not_watch'=>'tv','eat'=>'bread');
//better:
array(
'drink' => 'coffee',
'do_not_watch' => 'tv',
'eat' => 'bread',
);
Discussions and additional ideas are welcome!
Martin


