PHP Coding Standards and Naming Conventions

Best Coding Practice

Flow Chart for Project Decision Making

                       +---------+
                       |  START  | 
                       +---------+
                            |
                            V            
            YES       ----------------       NO
      _______________/  DOES THE DAMN \_____________               
      |              \   THING WORK ? /            |
      V               ----------------             V    
+------------+                               -------------- 
| DON'T FUCK |               _______________/ DID YOU FUCK \
| WITH IT    |               |              \   WITH IT?   /
+------------+               | YES           --------------
      |                      V                     |
      |               +-------------+              | NO
      |               |  YOU STUPID |              |
      |               |   DUMBSHIT  |              |
      |               +-------------+              |
      |                      |                     |
      |                 _____V_____                |
      |            NO  /           \               |
      |      _________/ DOES ANYONE \              |
      |      |        \    KNOW?    /              |
      |      |         \___________/               |
      |      |               |                     |
      |      |               | YES                 |
      |      |               V                     V
      |  +--------+   +-------------+  YES   ------------- 
      |  |  HIDE  |   |   YOU POOR  |/_____ /  WILL YOU   \
      |  |   IT   |   |   BASTARD   |\      \ CATCH HELL? /
      |  +--------+   +-------------+        ------------- 
      |      |               |                     |
      |      |               |                     | NO
      |      |               V                     V
      |      V        +-------------+       +------------+ 
      +-------------->|    STOP     |<------| SHITCAN IT |
                      +-------------+       +------------+
      

Conditions

If (not) empty

All conditions should like this

if ($var == '')

Or this one

if ($var == 0)

Should be

if (empty($var))

isset() vs empty()

Allways use !empty() instead of isset(), except in case whare allowed values is '' or 0

if (!empty($data)) { // do something }

Loop conditions

Try to avoid this

for ($i = 0; $i < strlen($sting); $i++) { // do something }

Use this instead

$l = strlen($sting); for ($i = 0; $i < $l; $i++) { // do something }

Because in first case condition will be calculated for each loop iteration.

Return Conditions

Try to simplify your code to use less lines specially for conditions like this:

if ($something > 45) { return true; } else { return false; }

This is nonsense and must allways be changed to:

if ($something > 45) { return true; } return false;

or on one line:

return ($something > 45) ? true : false;

or even better:

return $something > 45;

Appendix

PHPMyAdmin

Try to avoid PHP My Admin for table creation because it has problems with default values. It's great product for browsing and simple database manipulation but it makes you lazy and you usually forget to keep history of table alteration and other changes.

Document Date and Version

Document created in december 2008. Version 1.0 beta