phpRPG Coding Standards


"It helps if the standard annoys everyone in some way so everyone feels they are on the same playing field." - "C++ Coding Standard" by Todd Hoff


PHP FILE NAME
>> always use lower case letters when naming a file
>> use hyphens - as word separators
>> always use .php as the file extension
>> use .inc.php for include files
>> use .lib.php for library files and objects

Examples:

CORRECT
filename.php
battle-subsystem.php

INCORRECT
WrongFileName.php
still-no_good.php3



CODE STRUCTURE .php
>> make PHP parsing starts on the first line of code <?php till the end ?> of file
---->> to make file purely a PHP file, not a mixture with HTML
>> only one set of PHP tags are allowed - the start and the end of file
---->> using reversed tags ?> <?php as text output creates confusions
---->> use echo for printing instead
---->> print can be used to but is slightly slower
>> a header using comments tags with information about the function and requirements of the file should be included
---->> to report any parse warnings or errors, revealing potential bugs
---->> use @ to suppress warning messages from undefined variables if nessesary



CODE STRUCTURE .inc.php and .lib.php
>> follow the standards as above
>> contents within file should only be accessed by functions from main file
---->> for security measures
---->> executing a .inc.php or a .lib.php file should return null output without revealing its contents

Examples:

CORRECT
<?php
function printHello() {
	echo 'Hello';
}
?>

INCORRECT
<?php
echo 'Hello';
?>



FUNCTION NAMES
>> use upper case letters as word separators, lower case for the rest of the word
>> do not use underscores
>> do not use all caps despite if its an abbreviation, use an initial upper case followed by all lower case letters instead

Examples:

CORRECT
function fetchHtmlContent() {
	...
}

INCORRECT
function fetch_HTML_content() {
	...
}

function FetchHTMLContent() {
	...
}



ARGUMENT NAMES
>> first character should be lower case
>> all words beginnings after the first letter should be upper case

Example:

function startServer($serverName, $portToOpen) {
	return($serverName.':'.$portToOpen);
}



VARIABLE NAMES
>> use all lower case letters regardless
>> use underscores as word separators

Example:

function startServer($serverName, $portToOpen) {
	$new_server_name = 'www.'.$serverName;
	$server_argument = $new_server_name.':'.$portToOpen;
	return($server_argument);
}



ARRAY ELEMENTS
>> access an array element with single quotes
>> use underscores as word separators
>> do not use hyphens - as word separators
---->> so that an element can be accessed within magic quotes
---->> element name can be a variable within magic quotes

Examples:

$user['nick_name'] = 'Axolotl';
$user['nick-name'] = 'Axolotl';
$element_name = 'nick_name'

// output: Logged in as Axolotl
echo "Logged in as $user['nick_name']";

// will produce parse error
echo "Logged in as $user['nick-name']";

// output: Logged in as Axolotl
echo "Logged in as $user[$element_name]";



GLOBAL CONSTANTS
>> use all upper case letters
>> use underscores as word separators

Example:

define ('MYSQL_SERVER_NAME', 'mysql.sourceforge.net');



INDENTATION POLICY
>> indent using tabs for each level
>> do not use spaces

Example:

if ($condition) {
	while ($condition) {
	...
	}
}



BRACES { } POLICY
>> place braces inline with keywords (ie. if, while...)

Example:

if ($condition) {
	...
}
elseif {
	...
}
else {
	...
}



PARENTHESES ( ) POLICY
>> place parens next to functions
>> do not put parens next to keywords (ie. if, while...), put a space in between
>> do use parens in return statements

Examples:

CORRECT
function functionExample($sampleInput) {
	if ($condition) {
		...
	}
	return($sample_result);
}

INCORRECT
function FunctionExample ($sampleInput)  // parens not next to function
{
    if($condition)  // keyword is not a function
    {
        ...
    }
}



SUPERGLOBALS
>> all predefined variables must be accessed through super globals (ie. $_GET, $_POST, $_SESSION)
---->> so that all variables can be accessed regardless of the configuration
---->> better security as values of variables cannot be changed by passing values

Examples:

// ... = $test;
$test_value = $_POST['test'];

// ... = $PHP_SELF;
$current_url = $_SERVER['PHP_SELF'];



SQL QUERIES
>> use upper case letters for keywords
>> use upper case letters for SQL built-in functions
>> a query should not end with a semicolon ;

Example:

$sql = "SELECT COUNT(*) AS hit FROM user WHERE online > $range";
$sql = 'SELECT COUNT(*) AS hit FROM user WHERE online>'.$range;



USE SINGLE QUOTES
>> use single quotes whenever possible
---->> magic (double) quotes is slightly slower as PHP parses the string for variables
---->> use concatenation . when inserting variables into a string
---->> remember if you wish to use special characters (ie. '\n' '\t') then you must use magic (double) quotes.

Example:

echo 'Good morning! Today is ' . $day_of_week . '. Fine day!';
echo "\n Good morning! Today is $day_of_week. Fine day!";
