PHP – Best Practices and Coding Guidelines

PHP – Best Practices and Coding Guidelines

PHP is being used by 81.7% of internet, in websites that have known server side programming. Again, 98.7% of these PHP websites use the 5th version of the language. This shows that PHP 5.0 is arguable the top coding language today and the best examples lie in the form of,,,,, and more.

For someone looking into a career in PHP, the future seems bright. However, it is also important to understand you are able to maintain the high code quality, PHP’s naming conventions, visual styles, and other technical settings allows developers to come up with homogeneous codes that both easy to read and maintain. The experience and personality of the coder also comes into play when differentiating a code that is “okay” and a code that is “awesome”.


Naming conventions have always been undervalued by software developers. While no one denies that having a nice name makes things easy, many opt for cryptic abbreviations, juts to save some typing. It is generally advisable to have English names for class names, comments, method names, database tables field names and variable names. It is common consensus that English is always easier to remember than special characters.

Vendor namespaces

Vendor namespaces are the base for namespaces and package keys. This provides two advantages. Firstly, there isn’t an extra requirement for a central package key registry and secondly, it allows the coder to easily integrate third party packages like Zend framework components, Symfony2 components or for that matter, any PHP 5.3+ based library.

PHP – Best Practices and Coding Guidelines

Turn on Error Reporting

Error Reporting can be a highly useful function and enabled all throughout the development phase. Among the commonly used feature here would be E_ALL, spotting all critical errors with warnings. It is mandatory that we turn on this feature before putting our code to production.

Using the DRY Approach

DRY or ‘Do not Repeat Yourself’ ensures that there is no redundant code in our scripts. Codes that violate DRY are referred to as WET solutions and are common to programmers who enjoy unnecessary typing!

WET code:

$mysql  = mysql_connect ( ‘localhost’,  ‘mysqladmin_uid’, ‘mysqladmin_pwd’ );

mysql_select_db( ‘DB_NAME’ ) or die( “Sorry !! No database selected!”);


DRY version:

$db_host = ' localhost ';
$db_user = ' mysqladmin_uid ';
$db_password = ' mysqladmin_pwd ';
$db_database = ' DB_NAME ';
$mysql = mysql_connect($db_host, $db_user, $db_password);


Indentation / use of white space

Not just in PHP, but whatever language you may be working upon, it is necessary that the coding is properly indented and there is sufficient amount of white space wherever necessary. This helps in maintenance and enhancing the readability.

Don’t trust the user, ever!

If the application you are working on requires user inputs, make sure that it is designed to handle all kinds of possible inputs. Initializing your variables with some initial value can also be a tool against hackers, though it might not seem relevant to the business flow.

Lastly, use Cache whenever required and never resort to copying extra variables. These should keep your script sane and your application efficient.

Need Help? Chat with us