gtu php project training guidelines

19
GTU GUIDELINES FOR PHP CODING… By TOPS Technologies http://www.tops-int.com/php-training- course.html 9 / 1 1 / 2 0 1 3 1 T O P S T e c h n o l o g i e s : h t t p : / / w w w . t o p s - i n t . c o m / p h p - t r a i n i n g - c o u r s e . h t m l

Upload: neil-modi

Post on 29-Nov-2014

493 views

Category:

Education


1 download

DESCRIPTION

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.

TRANSCRIPT

Page 1: GTU PHP Project Training Guidelines

GTU GUIDELINES FOR PHP CODING…By TOPS Technologies

http://www.tops-int.com/php-training-course.html

9/1

1/2

01

3

1

TO

PS

Te

chn

olo

gie

s:http

://ww

w.to

ps-in

t.com

/ph

p-

train

ing

-cou

rse.h

tml

Page 2: GTU PHP Project Training Guidelines

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/1

1/2

01

3

2

TO

PS

Te

chn

olo

gie

s:http

://ww

w.to

ps-

int.co

m/p

hp

-train

ing

-cou

rse.h

tml

Page 3: GTU PHP Project Training Guidelines

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/1

1/2

01

3

3

TO

PS

Te

chn

olo

gie

s:http

://ww

w.to

ps-

int.co

m/p

hp

-train

ing

-cou

rse.h

tml

Page 4: GTU PHP Project Training Guidelines

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/1

1/2

01

3

4

TO

PS

Te

chn

olo

gie

s:http

://ww

w.to

ps-

int.co

m/p

hp

-train

ing

-cou

rse.h

tml

Page 5: GTU PHP Project Training Guidelines

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/1

1/2

01

3

5

TO

PS

Te

chn

olo

gie

s:http

://ww

w.to

ps-

int.co

m/p

hp

-train

ing

-cou

rse.h

tml

Page 6: GTU PHP Project Training Guidelines

EDITOR SETTINGS Tabs v. spaces Linefeeds

9/1

1/2

01

3

6

TO

PS

Te

chn

olo

gie

s:http

://ww

w.to

ps-

int.co

m/p

hp

-train

ing

-cou

rse.h

tml

Page 7: GTU PHP Project Training Guidelines

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/1

1/2

01

3

7

TO

PS

Te

chn

olo

gie

s:http

://ww

w.to

ps-

int.co

m/p

hp

-train

ing

-cou

rse.h

tml

Page 8: GTU PHP Project Training Guidelines

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 (\r\n). 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/1

1/2

01

3

8

TO

PS

Te

chn

olo

gie

s:http

://ww

w.to

ps-

int.co

m/p

hp

-train

ing

-cou

rse.h

tml

Page 9: GTU PHP Project Training Guidelines

NAMING CONVENTIONS

Variable names Loop indices Function names Function arguments

9/1

1/2

01

3

9

TO

PS

Te

chn

olo

gie

s:http

://ww

w.to

ps-

int.co

m/p

hp

-train

ing

-cou

rse.h

tml

Page 10: GTU PHP Project Training Guidelines

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/1

1/2

01

3

10

TO

PS

Te

chn

olo

gie

s:http

://ww

w.to

ps-

int.co

m/p

hp

-train

ing

-cou

rse.h

tml

Page 11: GTU PHP Project Training Guidelines

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/1

1/2

01

3

11

TO

PS

Te

chn

olo

gie

s:http

://ww

w.to

ps-

int.co

m/p

hp

-train

ing

-cou

rse.h

tml

Page 12: GTU PHP Project Training Guidelines

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_data(). 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/1

1/2

01

3

12

TO

PS

Te

chn

olo

gie

s:http

://ww

w.to

ps-

int.co

m/p

hp

-train

ing

-cou

rse.h

tml

Page 13: GTU PHP Project Training Guidelines

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/1

1/2

01

3

13

TO

PS

Te

chn

olo

gie

s:http

://ww

w.to

ps-

int.co

m/p

hp

-train

ing

-cou

rse.h

tml

Page 14: GTU PHP Project Training Guidelines

CODE LAYOUT

Including braces Where to put the braces Spaces between tokens Operator precedence SQL code layout

9/1

1/2

01

3

14

TO

PS

Te

chn

olo

gie

s:http

://ww

w.to

ps-

int.co

m/p

hp

-train

ing

-cou

rse.h

tml

Page 15: GTU PHP Project Training Guidelines

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/1

1/2

01

3

15

TO

PS

Te

chn

olo

gie

s:http

://ww

w.to

ps-

int.co

m/p

hp

-train

ing

-cou

rse.h

tml

Page 16: GTU PHP Project Training Guidelines

GENERAL GUIDELINES

Quoting strings Shortcut operators Optional shortcut constructs Use constants where possible Turn on all error reporting

9/1

1/2

01

3

16

TO

PS

Te

chn

olo

gie

s:http

://ww

w.to

ps-

int.co

m/p

hp

-train

ing

-cou

rse.h

tml

Page 17: GTU PHP Project Training Guidelines

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/1

1/2

01

3

17

TO

PS

Te

chn

olo

gie

s:http

://ww

w.to

ps-

int.co

m/p

hp

-train

ing

-cou

rse.h

tml

Page 18: GTU PHP Project Training Guidelines

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/1

1/2

01

3

18

TO

PS

Te

chn

olo

gie

s:http

://ww

w.to

ps-

int.co

m/p

hp

-train

ing

-cou

rse.h

tml

Page 19: GTU PHP Project Training Guidelines

FOR MORE VISIT US AT

• http://www.tops-int.com• http://www.tops-int.com/php-training-course.html

9/1

1/2

01

3

19

TO

PS

Te

chn

olo

gie

s:http

://ww

w.to

ps-

int.co

m/p

hp

-train

ing

-cou

rse.h

tml