PHP code guidelines

PHP code guidelines

This is the code guidelines for writing PHP code in Friend. Please adhere to these when submitting new code, or download a code formatter, available elsewhere on these developer pages.

General guidelines

When writing code in Friend, in PHP or otherwise, please make sure that you go ahead by respecting the following rules:

Make sure to comment each function using at least a single line comment like this:

	// This simple function takes an unformatted string and returns a formatted string

Write your function name using UCFirst, uppercase first:

	MySimpleFunction( $arg1, $arg2 )

Please use logical variable names if the variable does something special:

	$name = "Some Name"; v.s. $bleh = "Some Name" ($name is preferred!)

Bracket on newline

Where to place those brackets? The discussion has been going between programmers for a long time. Some prefer on the same line. Some prefer with a newline in front. We have to make a decision, and we chose to have the bracket after a new line like so:

	if( $theVar == 4 )
	{
		// Do something
	}

Spaces and tabs

It is allowed to use tabs on the beginning of a line, but not after a non space character:

	if( $here == 'there' )
	{
		// Tabbed -         spaced
		// Another tab -    space
	}

Also, notice the space after the paranthesis and the variable inside the if. We space our variables and parameters for good readability.

Single versus double quotes

When declaring or comparing strings, we use single quotes. Double quotes are used when the string includes formatting characters or nested variables.

	$string = 'This is a plain string.';
	$format = "This is a string which contains: $string\n";