PHP Coding Convention / Style

PHP being an open source product, lacks a formal and official coding convention. Different frameworks, CMS etc have come up with their own standards which in many cases are similar but at the same time do differ.

The convention mentioned below is mainly inspired from CodeIgniter documentation but does a very good job:

Class names
Class names should always start with a capital letter and multiple words should be separated by an underscore(_) letter. Example:

class Student_records { }

Method and Variable names
All method and variable names should be lowercase and separated by an underscore.

Private Methods and Variable names
Private members should additionally be prefixed with an underscore.

Constants
Constants should always be fully upper case and word should be separated by an underscore.

TRUE, FALSE and NULL
They should always be fully upper case.

Logical Operators
OR should be used instead of || and && should be preferred instead of AND. For negation there should be a space between ! and the word.

Comparison Operators
Always prefer the === and !== over == and != when comparing for boolean.

Whitespaces
For pure PHP files, there should be no whitespace before or after the php opening tags.

Strings
Always prefer single quotes over double quotes.

SQL Queries
SQL keywords should be capitalized like SELECT etc.

For a complete detailed description: http://codeigniter.com/user_guide/general/styleguide.html

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s