swape.net alireza balouch

13Nov/090

My PHP Code-Standards Part 1

Here is a tutorial on how to write a nice and clean PHP code.

Let us start with variables.

I use prefix for variables with camel notation. This makes it easy to see and check the right variable for its type.

  1. $strName = 'ali' ;
  2. $intCounter = 1;
  3. $arrData = array('a'=>'ali' , 'i'=>'is' , 'p'=>'perfect');
  4. $blnTrigger = true;
  5. $objMailSender = new mailSenderClass();

I use single quote for my data.

  1. $strMyNameIs = 'Alireza';

And I use double quotes for SQL or strings that I need to have a single quote inside.

  1. $strSQL = "SELECT * FROM person WHERE name LIKE 'ali' ";

I never use variable inside a double quote string. I break up the quote.

  1. $strCode = "some $bad code ";// WRONG AND UGLY
  2. $strNiceCode = 'Very ' . $strData . ' is pasted here' ;// NICE CODE

Never ever use short variables like $a or $temp or other lazy short names.
Use variable names that are understandable and describe what it contains.
And using a camelNotation makes it easy to read.
Never start with underscore or other kind of separator for names.

  1. $strReturn = 'Nice code';

Only use underscore for constants where you can not use tha camel notation.

  1. define('FOO_BAR','It works!');

And do not start with underscore. There are some other system variables and functions that starts with underscore like _get() or $_SESSION

blog comments powered by Disqus