My PHP Coding Standard

Before I start writing my own PHP tutorials, I would to set the coding standards I will be following in the course of this tutorial. This aims to standardize all my snippets ( small chunk of code) in all examples I will be giving. I would like to emphasize that this is not a standard to all PHP programmers but just my personal preferences which we all do have.

Comments – - comments are piece of information that programmers usually put to their code to explain what it does. Comments are not actually part of the execution of a program but instead they are ignored by the PHP interpreter. It is a good practice in a programmer to put comments on his code because in the future, comments are the only guide a programmer can follow to understand a big pile of code. Comments may vary depending on what type comment to put.

1. single line comments – for single line commenting I use the double forward slash //

Example:

// this line will print your name
print "My name is Juan dela Cruz";

2. multi-line comment – for multi line I use /* and the closing */

Example:

/*
This is a multi-line comment
and goes all the way
up to this point
*/

Variables

for variables I use the camelize form. Meaning letters in my variable have combination of upper and lower case. But in addition I am affixing a single letter prefix that will determine the datatype of that variable. For example, if I would declare an array variable I would write $aMyArray.

Example:

$aMyArray = array(1,2,3,4); // contains array values
$sYourName = "Conai"; // contains string values
$iLoop = 100; // contains integer values
$oDBConnection = mysql_connect("localhost", "username", "password"); // object
$bIsTrue = false;  // boolean
$mMixedVariable = "test"; // mixed variable, a variable that may vary in datatype

For single usage or single letter variable there is an exemption on my rule.

for ($x=1; $x<count($aMyArray); $x++) {
    <statement here>
}

in the above example $x dont have any prefix on what datatype it is. I have sited this as my exemption.



Constants

as a rule constants should all be in uppercase and words separated by underscore(_)

Example:

define('HOST',  'localhost');
define('USER_NAME', 'root');
define('PASSWORD', '1234567a');



Conditional Statements

if statement

if ($x == 1) {
    <statement here after 4 solid spaces>
}

if – else statement

if ($x == null || $y != false) {
    <statement of if after 4 solid spaces>
} else {
    <statement of else after 4 solid spaces>
}

if -else if – else

if ($x > count($aResults)) {
    <statement of if after 4 solid spaces>
} else if ($x < count($aResults)) {
    <statement of else fif after 4 solid spaces>
} else {
    <statement of else after 4 solid spaces>
}

Loop statement

for loop

for($iCounter=0; $iCounter<10; $iCounter++) {
    <statement of for loop after 4 solid spaces>
}

while

while( !feof($oHandle) ) {
    <statement of while loop after 4 solid spaces>
}

do-while

do {
    <statement after 4 solid spaces>
}  while( $iLoop > 10) ;


Functions - function names are the same as the camelize form of my variables. Function names usually starts with small letters.

Example:

function checkThisVariable($mVariable) {
     if( is_integer($mVariable) ) {
        return "integer";
    } else if ( is_array($mVariable) ) {
        return "array";
    } else {
         return "undetermine";
    }
}

Class - class name are also in camelize form but the first letter is Capitalize to signify that it is a class when placed among other variables.

Example:

class Cabinet {
    var $sDrawer;
    function Cabinet () {
        $this->sDrawer = true;
    }
}


								
				

			

Leave a Reply