Sunday, December 16, 2007

PHP coding tips

Since I'm so tired of seeing scrappy and illegible PHP code, I've decided to put up some tips for good and readable PHP coding.


* Always have function names and variable names in lower case, and any words in them separated by underscores


* Always capitalise

NULL


* Always capitalise constants

* Always use the proper C-style condition symbols, e.g.

&&

instead of

and

, and

||

instead of

or


* Always enclose separate condition clauses in brackets, e.g.

if(($a==1) || ($b==1))

, not

if($a==1 || $b==1)


* Always put block brackets on new lines, and indent the block contents, e.g.

if($a==1)
{

, not

if($a==1) {

, and

}
else
{

, not

} else {


* Always contract blocks when possible (if blocks with only one line of code in the block are a prime target)

Use contracted if statements when possible,

e.g.

($a=($b==1)?'hello':'goodbye';)