TOPS Technologies provides PHP training in Ahmedabad, for MCA Students PHP live project training as per GTU project guidelines. Get more info @ http://www.tops-int.com/, 903 Samedh Complex, Next to Associated Petrol Pump, CG Road, Ahmedabad 380009.
1. GTU GUIDELINES FOR
PHP CODING…
By TOPS Technologies
http://www.tops-int.com/php-training-course.html
9/11/2013
1
TOPSTechnologies:http://www.tops-int.com/php-
training-course.html
2. PHP CODING GUIDELINES
• These are the guidelines that you should follow
when writing your PHP scripts, unless a coding
standard already exists for the project working on. It
can be helpful to have something like this if you’re
working on a joint project.
• N.B. These are only the guidelines that we
personally chose to follow for the code when I was
student in GTU. This is not an indication that any
other coding styles, guidelines or standards are
wrong. Feel free to use this document as a
template to manage your own coding guideline and
change whatever you wish as you see fit.
9/11/2013
2
TOPSTechnologies:http://www.tops-
int.com/php-training-course.html
3. WHY ARE GUIDELINES
IMPORTANT?
• First of all, let’s make one point crystal clear: it
doesn’t matter what your guidelines are, so long as
everyone understands and sticks to them. I’ve
worked on a team where the person in charge
preferred to put braces following the expression,
rather than on a line all by themselves. Whilst I
didn’t necessarily agree with this convention, I stuck
to it because it made maintaining the whole project
much easier.
9/11/2013
3
TOPSTechnologies:http://www.tops-
int.com/php-training-course.html
4. WHY ARE GUIDELINES IMPORTANT?
• It cannot be emphasised enough that guidelines are
only useful if they are followed. It’s no use having a
definitive set of coding guidelines for a joint
programming project if everyone continues to write
in their own style regardless. It is arguable that you
can get away with different coding styles if every
team member works on a different section which is
encapsulated and therefore their coding style
doesn’t affect the other developers. Unfortunately,
this only holds true until a developer leaves and
someone else has to take over their role.
9/11/2013
4
TOPSTechnologies:http://www.tops-
int.com/php-training-course.html
5. WHY ARE GUIDELINES
IMPORTANT?
• If you are running a joint project, you might consider
putting your foot down and basically refuse to
accept any code that does not conform to your
published guidelines, regardless of its technical
merit (or lack thereof). This may sound somewhat
draconian and off-putting to developers at first, but
once everyone begins to code to the guidelines
you’ll find it a lot easier to manage the project and
you’ll get more work done in the same time. It will
require a lot of effort from some of your developers
who don’t want to abandon their coding habits, but
at the end of the day different coding styles will
cause more problems than they’re worth.
9/11/2013
5
TOPSTechnologies:http://www.tops-
int.com/php-training-course.html
7. TABS V. SPACES
• Ahh, the endless debate of tabs v. spaces. I used to
be a fan of tabs, but I’ve come round to the
argument that spaces are better — apart from
anything else you can guarantee that they will look
the same regardless of editor settings. The other
benefit to using two spaces (which is the number I
work with) is that code doesn’t start to scroll off the
right side of the screen after a few levels of
indentation.
9/11/2013
7
TOPSTechnologies:http://www.tops-
int.com/php-training-course.html
8. LINEFEEDS
• The three major operating systems (Unix, Windows and Mac OS) use
different ways to represent the end of a line. Unix systems use the
newline character (n), Mac systems use a carriage return (r), and
Windows systems use a carriage return followed by a line feed (rn).
If you’ve ever opened a file created in Windows on a Unix system,
you will probably have seen lots of odd characters (possibly
represented by ^M) where you would expect to see a clean line
break.
• I use simple newlines all the time, because the Windows way just
doubles the size of your line breaks and the Mac OS way is
technically incorrect in that a carriage return should only return you to
the beginning of the line, ala the old typewriter systems.
• If you develop on Windows (and many people do), either set up your
editor to save files in Unix format or run a utility that converts
between the two file formats.
9/11/2013
8
TOPSTechnologies:http://www.tops-
int.com/php-training-course.html
9. NAMING CONVENTIONS
Variable names
Loop indices
Function names
Function arguments
9/11/2013
9
TOPSTechnologies:http://www.tops-
int.com/php-training-course.html
10. VARIABLE NAMES
• A lot of textbooks (particulary those about Visual C++) will try to
drum Hungarian notationinto your head. Basically, this means having rules
such as pre-pending g_ to global variables, i to integer data types etc. Not
only is a lot of this irrelevant to PHP (being a typeless language), it also
produces variable names such as g_iPersonAge which, to be honest, are not
easy to read at a glance and often end up looking like a group of random
characters strung together without rhyme or reason.
• Variable names should be all lowercase, with words separated by
underscores. For example, $current_user is correct,
but $currentuser, $currentUser and $CurrentUser are not.
• Names should be descriptive, but also concise. Wherever possible, keep
variable names to under 15 characters, although be prepared to sacrifice a
few extra characters to improve clarity. There’s no hard and fast rule when it
comes to the length of a variable name, so just try and be as concise as
possible without affecting clarity too much. Generally speaking, the smaller
the scope of a variable, the more concise you should be, so global variables
will usually have the longest names (relative to all others) whereas variables
local to a loop might have names consisting only of a single character.
• Constants should follow the same conventions as variables, except use all
uppercase to distinguish them from variables. So USER_ACTIVE_LEVEL is
correct, but USERACTIVELEVEL oruser_active_level would be incorrect.
9/11/2013
10
TOPSTechnologies:http://www.tops-
int.com/php-training-course.html
11. LOOP INDICES
• This is the only occassion where short variable names (as
small as one character in length) are permitted, and indeed
encouraged. Unless you already have a specific counting
variable, use $i as the variable for the outermost loop, then go
onto $j for the next most outermost loop etc. However,
do not use the variable $l (lowercase ‘L’) in any of your code
as it looks too much like the number ‘one’.
• Example of nested loops using this convention:
• for ( $i = 0; $i < 5; $i++ ) { for ( $j = 0; $j < 4; $j++ ) { for ( $k =
0; $k < 3; $k++ ) { for ( $m = 0; $m < 2; $m++ ) { foo($i, $j, $k,
$m); } } } } If, for some reason, you end up nesting loops so
deeply that you get to $z, consider re-writing your code. I’ve
written programs (in Visual Basic, for my sins) with loops
nested four levels deep and they were complicated enough. If
you use these guidelines in a joint project, you may way to
impose an additional rule that states a maximum nesting
of xlevels for loops and perhaps for other constructs too.
9/11/2013
11
TOPSTechnologies:http://www.tops-
int.com/php-training-course.html
12. FUNCTION NAMES
Function names should follow the same
guidelines as variable names, although they
should include a verb somewhere if at all
possible. Examples
include get_user_data() andvalidate_form_d
ata(). Basically, make it as obvious as
possible what the function does from its
name, whilst remaining reasonably concise.
For
example,a_function_which_gets_user_data
_from_a_file() would not be appropriate!
9/11/2013
12
TOPSTechnologies:http://www.tops-
int.com/php-training-course.html
13. FUNCTION ARGUMENTS
Since function arguments are just variables used in
a specific context, they should follow the same
guidelines as variable names.
It should be possible to tell the main purpose of a
function just by looking at the first line,
e.g. get_user_data($username). By examination,
you can make a good guess that this function gets
the user data of a user with the username passed
in the $username argument.
Function arguments should be separated by
spaces, both when the function is defined and when
it is called. However, there should not be any
spaces between the arguments and the
opening/closing brackets.
9/11/2013
13
TOPSTechnologies:http://www.tops-
int.com/php-training-course.html
14. CODE LAYOUT
Including braces
Where to put the braces
Spaces between tokens
Operator precedence
SQL code layout
9/11/2013
14
TOPSTechnologies:http://www.tops-
int.com/php-training-course.html
15. SQL CODE LAYOUT
• When writing SQL queries, capitialise all SQL
keywords (SELECT, FROM, VALUES, AS etc.) and
leave everything else in the relevant case. If you
are using WHERE clauses to return data
corresponding to a set of conditions, enclose those
conditions in brackets in the same way you would
for PHP if blocks, e.g. SELECT * FROM users
WHERE ( (registered = 'y') AND ((user_level =
'administrator') OR (user_level = 'moderator')) ).
9/11/2013
15
TOPSTechnologies:http://www.tops-
int.com/php-training-course.html
16. GENERAL GUIDELINES
Quoting strings
Shortcut operators
Optional shortcut constructs
Use constants where possible
Turn on all error reporting
9/11/2013
16
TOPSTechnologies:http://www.tops-
int.com/php-training-course.html
17. SIDE EFFECTS OF SHORT-CIRCUIT
LOGIC EVALUATION
One feature of PHP that can catch even expert developers
out, as well as being hard to track down, is the shortcuts
taken when evaluating boolean expressions. This is when
PHP stops evaluating a boolean expression part way through
because it already knows the result. For example, if you have
two expressions combined with the && (AND) operator, you
know that if the first expression is false then the whole thing
must be false because anything AND’d with false is false.
This short-circuit evaluation can catch you out if one or more
of the expressions performs and operation as part of being
evaluated. For example, $a = 5 sets the value
of $a to 5 andevaluates to 5. The safest route is to perform all
of your operations first, store any results in variables and then
evaluate them.
9/11/2013
17
TOPSTechnologies:http://www.tops-
int.com/php-training-course.html
18. BIO
In detailed knowledge and guidelines kindly visit us
at your nearest center.
You can visit us at
• http://www.tops-int.com
• http://www.tops-int.com/php-training-course.html
We also Do live project training, free workshop and
seminar hosted by our former students and
faculties every Saturday to share their knowledge
and experience with newbie's.
9/11/2013
18
TOPSTechnologies:http://www.tops-
int.com/php-training-course.html
19. FOR MORE VISIT US AT
• http://www.tops-int.com
• http://www.tops-int.com/php-training-course.html
9/11/2013
19
TOPSTechnologies:http://www.tops-
int.com/php-training-course.html