Coding Standards

Read {count} times since 2020

Writing programs should be meaningful and in a long future, if you or anyone looks back at the code, then he/she must understand it. We all write codes, but do we have a standard of writing the code ? If we write codes without any standards and just go for it, then the program would be messy and no other programmer would like to read it.

Here’s what I’m talking about : A guy writes code as he like :

//A function to forget. Just a demo function for showing code standards. Nothing is Nothing.
function forget ($get=false) {
echo $get===true ? "Yes":"No";

The above code is messy and doesn’t look good and here’s how it is made better :

 * A function to forget. Just a demo function for
 * showing code standards. Nothing is Nothing.
function forget($get = false) {
  echo $get === true ? "Yes" : "No";

You can see that, now the code looks better and gives a temptation to read it. Not only does it help you, but others who might read the code in the future as well.

This post is about the coding standards that I use and maybe you could use it too ! This coding standards is loosely based on the PEAR coding standards.

The PHP File

On most PHP systems, <?php works perfectly, but on others the short code is not enabled. In this case, <? won’t work. So, starting of PHP code should be by <?php :

echo "Hello World";

If the file is full PHP, then it need not be closed by ?>. All files should end with a newline (\n) and not Windows newline (\r\n).

Semicolons is important. It should be placed. Do this :

<?php echo "hello"; ?>

and not this :

<?php echo "hello" ?>

Indents & Spacing

The indents should be of 2 spaces and no tabs. Tabs won’t work equally perfect in all text editors. Hence, instead of tabs, whitespaces should be used.

Lines should not have trailing whitespaces at the end. A space should be in between operators such as :

+, =, -, !=, !==, >

etc… Examples :

$myVariable = "myContent";
if(5 < 4) { }

rather than :


Also, a space after "," should be made.

Control Structures

Control structures include if, for, while, switch, etc… Here is an example of if statement :

if (5 > 4 || 9 > 8) {
}elseif ("a" == "a") {

Always use elseif and not else if. For siwtch statements :

switch (condition) {
  case 1:

  case 2:



All lines should not exceed 80 characters. If it does, make the overflowing chars into next line :

if($array['this']['that']['here']['pen'] ==
$array['there']['that']['myBigHat']['insideAPen']) {

This applies to comments too :

 * A function to forget. Just a demo function for
 * showing code standards. Nothing is Nothing.

There might be a need to extend from 80 characters if it is a control structure. In this case, we can on one condition : it must be understandable.

Another thing is that all lines that are kept out for prettiness mustn’t have any characters, even whitespaces.

Variables & Functions

As said before, a space should be in between ‘=’ in variables :

$var = "content";
$var .= "content";

In case of variables that are together of different sizes, adding more space will help to read it better :

$name                 = "";
$fullName             = "";
$aLitttleLongVariable = ""; 

In the case of functions :

myFunction($var1, $var2);

Functions should return meaningful data. It’s better to avoid NULL as return and instead use boolean false. Use :

function myFunction() { return true; }

and not :

function myFunction() { return "true"; }

or :

function myFunction() { return TRUE; }


The items of arrays should be separated with whitespace and inbetween "=>" :

$myArray = array('hello', 'world', 'me' => 'him');

It can be also made more pretty by :

$myArray = array(
 'hello' => 'a',
 'world' => 'b',
 'me' => 'him'


It’s better to use double quotes (") in PHP, but both single quote and double quote have good uses.


Comments should start by "/*" and end with "*/". Each line of comment must start with "" too :

 * Reads configuration & set Software according to it
 * All lines must start with *

This also have the 80 chars limit.


All function names and variable names must be in lowercase and words should be separated by underscore (_) or you can do the caps way :


But, your project must only use one of the above methods.


It’s better if all your projects follow the same coding standards as it will help you and other who read the code as well. This coding standards are my opinion only. You can choose your own and write it down in a document or on your mind and follow it forever in your coding life.

Show Comments