Coding Guidelines (UseBB 1)
Standard file templates
All pages should be in Unix format (\n for newlines). Please use a text editor set up to use the Unix format.
A normal page
The following is the template for a normal (directly accessible) page in UseBB.
<?php /* Copyright (C) 2003-2007 UseBB Team http://www.usebb.net $Header: $ This file is part of UseBB. UseBB is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. UseBB is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with UseBB; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ define('INCLUDED', true); define('ROOT_PATH', './'); // // Include usebb engine // require(ROOT_PATH.'sources/common.php'); // // Update and get the session information // $session->update('pagename'); // // Include the page header // require(ROOT_PATH.'sources/page_head.php'); ### The page's contents comes here ### ### (Delete this note) ### // // Include the page footer // require(ROOT_PATH.'sources/page_foot.php'); ?>
An included page
The following is the template for an included page in UseBB.
<?php /* Copyright (C) 2003-2007 UseBB Team http://www.usebb.net $Header: $ This file is part of UseBB. UseBB is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. UseBB is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with UseBB; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ // // Die when called directly in browser // if ( !defined('INCLUDED') ) exit(); ### The page's contents comes here ### ### (Delete this note) ### ?>
Note: don’t forget the ?> and to put a newline character (\n) after the ?>!
Variables
Variable naming
Variables are need to be named clearly and understandable. Variable names can contain a single word or multiple words joined with an underscore. For example:
$variable $another_variable
These are legimate names. The following are not:
$anothervariable $anotherVariable
Variable initialization
Used variables always need to be initialized first. For example:
$variable = array(); foreach ( $this as $that ) $variable[] = $that;
Also, !empty() needs to be used first when checking a variable for anything else in an IF expression. For example:
if ( !empty($_POST['user']) && preg_match(USER_PREG, $_POST['user']) )
The default error reporting in UseBB (E_ALL) should produce an error when a variable was not initialized before usage. For security reasons, never change the error reporting level when developing!
Indentation and whitespace
Note: All examples contain certain whitespace. Please beware of them and use them in your code! The usage of whitespace also counts as a coding guideline!
Indentation in UseBB is done with tabs, no spaces. Indentation follows when we are inside a loop (foreach, while, ...) and in an if or else clausule. For example:
if ( !empty($this) ) { $this = htmlentities($this); $that = trim($that); } else { $this = 'this'; }
Note we place empty lines after the line ending with { and before the line starting with }, and that those lines also contain indentation.
When using no { and }, which is preferred for a single statement inside the loop or clausule, no empty lines are needed. For example:
if ( !empty($this) ) $this = htmlentities($this); else $this = 'this';
Whitespace is needed before and after the brackets in an if, foreach, while, ... expression. Also, there needs to be a space before and after curly braces (except when the brace is the first or the last character on the line - tabs not included). See the previous examples.
A closing curly brace (}) is always placed at the same indentation as where the clausule started. See the previous example.
Also, array elements are indented:
array( 'key' => 'value'; );
Ternary operator
The use of the ternary operator (short form of IF) is allowed for assigning a value to a variable. For example:
$some_variable = ( !empty($another_variable) ) ? $another_variable : '';
Note the condition of the expression needs to be surrounded by brackets. Also note the whitespace used in the statement.
