Académique Documents
Professionnel Documents
Culture Documents
Document History
1. OVERVIEW
2. SCOPE
3. GLOSSARY OF TERMS
4. STANDARDS
4.5 Comments
4.5.1 Definition
4.5.2 Rules To Follow
4.5.3 Description
4.5.4 Example
4.5.5 Warning
6. ANNEXURE
1. Overview
This document basically describes the coding standards and conventions to be followed when
developing a PHP code.
2. Scope
The scope of this document extends to all the projects developed in iProgrammer using PHP as
the web programming language and to all the developers who are going to develop those
projects. Some of the standards defined this document are generic and may imply to any
programming language.
3. Glossary of Terms
Term/Abbreviation Description
CL Client
PM Project Manager
DB Database
CS Coding Standards
CC Coding Conventions
FS Folder Structure
NC Naming Conventions
4. Standards
4.1.1 Definition
The following folders have been identified as part of the web development process:
• includes
• js
• css
• images
• cron
• functions
• process
• admin
• others
• includes: This folder will include all the configuration files, error handling class,
database class and any other classes if at all required. There will also be a
config.inc.php file that will hold all the configuration constants of the site. This file
should not be uploaded directly but rather a continuous effort should be made to re-
modify it every time we need to change its contents so that we don’t overwrite the
previous data contained in it.
• js: All the java script files will be stored here
• css: All style sheets will be a part of this folder
• images: As the name suggests all the site images should be in this folder including
flash
• cron: A cron is a program that is scheduled to run periodically. Any cron script should
get into this folder. The folder should be protected by htaccess so that no one can
directly run the scripts contained in that folder. This folder is optional.
• functions: All the common PHP functions file, user defined functions file etc should
come into this folder.
• process: All the business logic of all the form contained pages should be stored in the
files contained in this folder. Example. Suppose you have a login.php page in the
member folder. Then the business logic to validate the user name and password will be
written in a different file called login.inc.php (assuming that you are submitting the
form to the same page) but stored in the folder named process. Refer the example
part of an actual example.
• admin: All the administrative files will be stored in the admin folder. This folder also in
turn can have sub-folders i.e. the above 7 folders.
• others: The others folder is a general term for many other folders which can be a part
of your project.
Example:
1. registration: All the registration related files and pages should be stored in this
folder.
2. member: If the site contains a member section or any other general public section
then those files should be kept here. This folder in turn can contain other folders
like reports, accounts, billing, payment etc depending on the project
requirement.
4.1.4 Example
<?php
####################################
# File Name: member/login.php
# Created By: XYZ
# Created On: June 21, 2005
# This file will contain the login form.
####################################
And the actual validation file login.inc.php but in process folder would look like this:
<?php
####################################
# File Name: process/login.inc.php
# Created By: XYZ
# Created On: June 21, 2005
# This file will contain the business logic to validate the
# username and password submitted from form contained
# in member/login.php file.
####################################
if ($_POST[‘btnSubmit’] != “”)
{
//Your validation code
}
?>
4.1.5 Warning
• When creating any ‘other’ folder apart from the standard (1-7) folders, it is required
that you consult your TL or PM before doing so
4.2.1 Definition
• Indentation refers to the spacing of the code syntax, which makes it easier to read and
understand the code.
• Line length refers to the length of the code statement in a single row/line.
• Begin writing your code with an indent of a single tab (i.e. nth column of any row
depending on the editor. In case of Zend, the 5th column. Always use tabs wherever
and whenever needed. Do not use white spaces). But the opening php tag “<?php”
should begin on the first column itself and the closing php tag “?>” should end on the
first column itself.
• All the control structures’, functions’, classes’ etc opening and closing braces should
start and end on the same column number.
• It is recommended that you break lines at approximately 75-85 characters. There is no
standard rule for the best way to break a line.
4.2.3 Description
• Your code should be well intended so that it looks neat and tidy. Anybody who sees
your code should be able to easily identify the placing of various blocks. Even when
you review the code after few days it will be easy for you to identify things within the
code. Some standard editors like Zend have built in facility to indent your code. You
just need to select your entire code and choose the option to intend your code from
the menu options
4.2.4 Example
Row/Column 1 2 3 4 5 6 7 8 9 10 11 12 13 14……………..
1 <?php //starts at 1st column
2
3 //starts at 5th column i.e. an indent of single tab (Zend editor).
4 For ($I=1; $I <= 5; $I++)
5 {
6 For ($j=1; $j <= $I; $j++)
7 {
8 Print “*”;
9 }//notice where the braces start and where they end.
10
11 echo “<br>”;
12
13 } //notice where the braces start and where they end.
14
15 if ($I == 6)
16 {
17 echo “The design is complete” .”<br><br>”;
18 }
19 echo “Bye! Have a good day.”;
20
21 ?> //ends on 1st column
4.3.1 Definition
• For controlling the logic of any application we require control structures which includes
statements like “if”, ”for”, ”while”, “switch” etc.
• Control statements should have one space between the control keyword and opening
parenthesis, to distinguish them from function calls.
• Always use curly braces even in situations where they are technically optional. Having
them increases readability and decreases the likelihood of logical errors being
introduced when new lines are added.
• For if statements if there are multiple conditions, then each condition should be
enclosed in round braces.
• If there is a single condition inside an if-else statement then use of ternary operators
(? :) is always recommended. Ternary operators are generally used when you need to
collect a specific integer or float value depending on the if-else condition.
• Switch statements are always preferred over "if elseif elseif else" kind of statements.
• Switch statements should always have a default condition.
4.3.3 Example
$intCnt = 1;
$boolFlag = true;
if (($intTotalRecCount % $intPageSize) == 0)
{
$intPageCount = $intTotalRecCount / $intPageSize;
}
else
{
$intPageCount = ceil($intTotalRecCount / $intPageSize);
}
<?php
//Comparison of “if elseif elseif else” and switch statements
$intCnt = 1;
if ($intCnt == 1)
{
echo “One”;
}
elseif ($intCnt == 2)
{
echo “Two”;
}
elseif ($intCnt == 3)
{
echo “Three”;
}
else
{
echo “None”;
}
Switch ($intCnt)
{
case “1” :
echo “One”;
break;
case “2” :
echo “Two”;
break;
case “3” :
echo “Three”;
break;
default :
echo “None”;
}
//Notice of the placement of the “case” keyword, the colon “:” , the
// “echo” statements and the “break” statements.
?>
4.3.4 Warning
4.4.1 Definition
• Function calls and declarations refer to the way of calling a function definition and the
actual declaration of the function.
• Functions should be called with no spaces between the function name, the opening
parenthesis, and the first parameter, spaces between commas and each parameter,
and no space between the last parameter, the closing parenthesis, and the semicolon.
Here's an example:
$intRetVal = fnGetName($intCustID, $strEmail, $strZip);
• All function names should begin with “fn” followed by the actual function name with
each breaking work in capital.
• Arguments with default values should always go at the end of the argument list.
• Always attempt to return a meaningful value from a function, the one that is
appropriate.
4.4.3 Example
<?php
//observe the following in the function definition
#####################################################
#1. The function name. Starts with fn and the actual function name with each
# breaking word in capital i.e. Compare,Two,Numbers.
#2. How the function is called. Refer point 1.
#3. Argument with default value at the end of the list.
#4. Value returned if its meaningful or not.
#####################################################
$intVal1 = 3;
$intVal2 = 5;
$intGreater = fnCompareTwoNumbers($intVal1,$intVal2=’8’);
echo “The greater of ” .$intVal1 .” and ” .$intVal2 .“ is ” .$intGreater;
function fnCompareTwoNumbers($intNum1,$intNum2=’0’)
{
if ($intNum1 > $intNum2)
{
$intGreater = $intNum1;
return $intGreater;
}
else
{
$intGreater = $intNum2;
return $intGreater;
}
?>
4.4.4 Warning
• Make sure the function parameters are explicitly verified in the code, arrays are
checked for out-of-bound indexes, variables are initialized before they are used etc.
4.5 Comments
4.5.1 Definition
• Comments form one of the most important aspects of the software development
process. It should be a must to comment your code at regular steps and stages.
Consider your comments a story describing the system. Page description, loop
comments, class comments are one part of the story, method signature comments are
another part of the story, method arguments another part, and method
implementation yet another part. All these parts should weave together and inform
someone else at another point of time just exactly what you did and why
4.5.3 Description
• Commenting PHP page - Each php page should have the following information
header comment at the very start. The name of the page, its author, creation date,
last modified by person’s name, last modified date and a brief description of what logic
the page contains.
• Commenting Functions - Even functions should be well commented with details like
who created the function, when was it created, why was it created, what are the
parameters and their data types and what is the return value of the function. If the
function is big and complex then it should internally contain inline or block comments.
• Internally your page/function can have two types of comments:
a) Inline Comments - These are single line comments describing the current
line.
Syntax: // this is a single line comment
b) Block Comments - These are used to describe the next 4-5 lines of code (or
may be even more) that logically go together.
Syntax: /*-----------------------------------------------------------------
Description of the code block goes here…
-------------------------------------------------------------------*/
4.5.4 Example
• Commenting Functions
<?php
#####################################################
# Function Name: void fnLogin($strUserName = 'nobody', $strUserPass = 'nobody')
# Created By: ABC XYZ
# Created on: 24th June 2005
# Purpose: Member function to validate the user login process
# Parameters: string $strUserName: User’s login name
# string $strUserPass: User’s Password
# ON SUCCESS: Sets the session variable $_SESSION[‘IsLoggedIn’] to a value of 1
# and returns 1
# ON FAILURE: Returns 0 on failure.
#####################################################
?>
• Inline Comment
<?php
• Block Comment
<?php
//Good block comment (describes the problem well)
/*---------------------------------------------------------------------------------------
4.5.5 Warning
You are forced to use block comment to describe any logical block of the script
• Don't use block comment every 2 lines of code - TOO OFTEN
• Don't use block comment every 200 lines - NOT ENOUGH
4.6.1 Definition
• This standard explains how to use PHP tags and include statements.
• PHP tags
• Other Include Statements
4.6.3 Description
• Always use <?php ?> to delimit the PHP code and not the <??> shorthand. This is
required for PEAR compliance and is also the most portable way to include PHP code
on differing operating systems and setups.
4.6.4 Example
• include($_CONF['PATH'] . "includes/myfunctions.php");
4.6.5 Warning
4.7.1 Definition
• We should always follow the Hungarian (also called Camel Case) naming
convention.
• Naming a Class
• Naming Functions and Methods
• Naming Constants
• Predefined Variables
• Variables
• File Naming Conventions
• Form Elements
4.7.3 Description
• Variables –
• All the variable names must be in bumpy or camel case as mentioned
above.
• Separate words and parts of variable name with capitalizing first letter of
each word i.e. simply capitalizing the first alphabet of each breaking word.
• Loop variables should be denoted by $i, $j, $k and so on.
• $sqlQuery1,$sqlQuery2 etc. and $resResult1, $resResult2 etc. are used for
sql-query string and resource identifier respectively.
• The chart below explains naming variables based on different data types:
4.7.4 Example
1. Naming a Class –
Examples of good class names are:
• clsStudent
• clsError
• clsMail
4.7.5 Warning
• The true, false and null constants are exempted from the all-uppercase rule, and
must always be lowercase.
• For a detailed description and usage of the pre defined variables please refer the
PHP manual.
4.8.1 Definition
• This section explains all the database standards and conventions to be followed
while designing a database and using that database in your application.
4.8.3 Description
• Table Fields
1. Field names should be all UPPER CASE and each word should be separated by
underscore.
4.8.4 Example
• Documenting Tables
tblRestaurant
Field Name Data Type Comments
PK RESTAURANT_ID INT (AUTO)
FK to CUST_ID in
FK CUST_ID INT (4)
tblCustomer
RESTAURANT_NAME VARCHAR (200)
ADDRESS VARCHAR (100)
CITY VARCHAR (50)
FK to STATE_ID in
FK STATE INT (2)
tblState
4.8.5 Warning
Bloopers:
• The best way to learn progressively is from past mistakes. This section discusses
some bloopers from our own past documents.
1. tblUserMaster
Field Name Data Type Remarks
USER_ID INT (Identity)
USER_LOGIN VARCHAR (100) Email id of the user
USER_PWD VARCHAR (8) Combination of characters
USER_TYPE VARCHAR (1) ‘S’ – SHEA users
‘O’ - Others
Remarks:
• Name of the table is incorrect. The use of “Master” is outdated. The correct table
name is “tblUser”.
• In the above table, every field name contains the suffix “USER”. This is a wrong
practice. Here are field equivalents:
a. USER_ID = USER_ID
b. USER_LOGIN = LOGIN_NAME
c. USER_PWD = PWD
d. USER_TYPE = TYPE
• As far as possible, use full nouns. PWD should be PASSWORD
• The remark for the field USER_PWD is not very descriptive. It should read – “User’s
Password. It can contain alphabets, numbers, special characters.”
6. Annexure
None