Utopia News Pro is coded according to the following PHP standards.

Table of Contents
  1. Braces
  2. Indenting
  3. Loops and Branches
  4. Spacing
  5. Quoting
  6. Boolean Function Return Values
  7. AND/and/&& OR/or/||
  8. Comments
  9. SQL Query Syntax
  10. Function Naming
  11. Miscellaneous

Color Examples Key:

$code = do_function($var); // blue: code like this

$code = do_function($var); // red: do not code like this
Braces

Braces should always be placed on a new line.

if ($condition1)
{
	// condition1 is true - do something
}
elseif ($condition2)
{
	// condition2 is true - do something else
}
else
{
	// neither is true - do another thing
}

Always use braces even when a loop or branch contains just one line of code.

if ($condition1)
	// condition1 is true - do something
else
	if ($condition2)
		// condition2 is true - do something
	else
		// neither is true - do another thing


if ($condition1)
{
	// condition1 is true - do something
}
else
{
	if ($condition2)
	{
		// condition2 is true - do something
	}
	else
	{
		// neither is true - do another thing
	}
}
Indenting

When indenting use a single tab.

function unp_isValidEmail($email)
{
	if (eregi('^[a-zA-Z0-9_\-\.]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$', $email))
	{
		return true;
	}
	else
	{
		return false;
	}
}
Loops and Branches

Loops and branches should be followed by a space before the opening parenthesis.

if ($action == 'main')
{
	// do something
}

if($action=='main')
{
	// do something
}

for ($i = 0; $i < 10; $i++)
{
	// do something
}

for($i=0;$i<10;$i++)
{
	// do something
}

while ($resultset = $DB->fetch_array($query))
{
	// do something
}

while($resultset=$DB->fetch_array($query))
{
	// do something
}
Spacing

Operators, except for ++ and --, should be surrounded with spaces.

$var1 = 10;

$var2 = 80 / 20;

$var3 = 3 * 6;

$var4 = (3 * 6) / 2;

$var5++;

$var6--;

Function calls should not have a space before opening parentheses.

$str = preg_replace('/\[b\](.+)\[\/b\]/is','<strong>\\1</strong>',$str);

Function arguments should have a space after each comma.

$str = str_replace($var1, $var2, $var3);

There should be no unnecessary spaces around parentheses.

$str = stripslashes($str);
$str = addslashes( $str );
Quoting

Strings should be quoted using single quotes. If they have control characters, escape them. If they have special entities (such as line breaks \n), concatenate the string and use double quotes to contain them. If they have variables, concatenate the string with the variable.

$str1 = 'Hello World!';
$str2 = 'Hello, World!'."\n\n"; // concatenation with special entities & double strings
$str3 = 'Hello, World! My name is '.$name; // concatenation with variable
$str4 = 'Your PHP version is '.phpversion().'<br />';

MySQL queries should always be in double quotes.

$DB->query("SELECT COUNT(*) AS NumResults FROM unp_table");
$DB->query('SELECT COUNT(*) AS NumResults FROM unp_table');

Variables that do not need to be quoted should not be.

$var = $var;
$var2 = addslashes($var2);

$var = "$var";
$var2 = addslashes("$var2");
Boolean Function Return Values

Unless there is very good reason to use 0 and 1, such as if there is the possibility of a 2 return value, use true and false.

function is_ten($int)
{
	if ($int == 10)
	{
		return 1;
	}
	else
	{
		return 0;
	}
}

function is_ten($int)
{
	if ($int == 10)
	{
		return true;
	}
	else
	{
		return false;
	}
}

Use the lowercase forms of true and false rather than TRUE and FALSE. Uppercase words are generally reserved for constants.

if ($var == TRUE)
{
	// do something
}

if ($var == true)
{
	// do something
}
AND/and/&& OR/or/||

Use '&&' instead of 'AND/and' and '||' instead of 'OR/or'.

if ($var == true && $var2 == true)
{
	// do something
}
if ($var == true || !$var2)
{
	// do something
}

if ($var == true AND $var2 == true)
{
	// do something
}
Comments

Use the following comment as a header for large chunks of code or branches.

// +------------------------------------------------------------------+
// | Do Operation                                                     |
// +------------------------------------------------------------------+

Use the // comment style for single line/short comments, and the /* ... */ syntax for large block comments.

// this is a short single line comment

// this is a fairly short comment
// that spans two lines

/*
 * this is a long
 * block style comment.
 * it spans multiple lines
 * and my go on and on
 */
 
 // this is a long
 // block style comment
 // that is using a non-standard
 // method of commenting

Prefix function definitions with the following comment string.

/***************************************************************
   Start Check Vars are The Same
***************************************************************/
function do_something($var1, $var2)
{
	if ($var1 == $var2)
	{
		// the same
		return true;
	}
	else
	{
		// different
		return false;
	}
}
SQL Query Syntax

SQL queries should always be double quoted, regardless of whether or not the query contains dynamic content.

$DB->query("SELECT * FROM `table` WHERE id='$someid'");
$DB->query("SHOW TABLE STATUS");

$DB->query('SELECT * FROM `table` WHERE id='.$someid);
$DB->query('SHOW TABLE STATUS');

In addition, table names should be enclosed within ` type quotes and values should be enclosed within ' quotes.

$DB->query("SELECT * FROM `table` WHERE id='$someid'");

$DB->query("SELECT * FROM table WHERE id='$someid'");
Function Naming

Custom written functions for UNP should be, for the most part, prefixed with "unp_".

function unp_lettersOnly($str)
{
	$str = preg_replace('^[^a-zA-Z]+$','', $str);
	return $str;
}

Functions should be named something useful so that someone could be able to have a general idea of what the function does just by looking at the name.

Miscellaneous

When passing a variable through a URL, use the following statement to retrive it (example assumes variable is "action"). The longer method should not be used.

isset($_GET['action']) ? $action = $_GET['action'] : $action = '';

if (isset($_GET['action']))
{
	$action = $_GET['action'];
}
else
{
	$action = '';
}
Top