SlideShare uma empresa Scribd logo
1 de 19
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
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
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
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
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
EDITOR SETTINGS
 Tabs v. spaces
 Linefeeds
9/11/2013
6
TOPSTechnologies:http://www.tops-
int.com/php-training-course.html
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
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
NAMING CONVENTIONS
 Variable names
 Loop indices
 Function names
 Function arguments
9/11/2013
9
TOPSTechnologies:http://www.tops-
int.com/php-training-course.html
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
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
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
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
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
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
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
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
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
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

Mais conteúdo relacionado

Destaque

5. mrtg in nagios1 0
5. mrtg in nagios1 05. mrtg in nagios1 0
5. mrtg in nagios1 0
aqpjuan
 
Plugging Network Security Holes Using NetFlow
Plugging Network Security Holes Using NetFlowPlugging Network Security Holes Using NetFlow
Plugging Network Security Holes Using NetFlow
NetFlow Analyzer
 

Destaque (19)

Php project training in ahmedabad
Php project training in ahmedabadPhp project training in ahmedabad
Php project training in ahmedabad
 
PHP Project development with Vagrant
PHP Project development with VagrantPHP Project development with Vagrant
PHP Project development with Vagrant
 
Introduction to php web programming - get and post
Introduction to php  web programming - get and postIntroduction to php  web programming - get and post
Introduction to php web programming - get and post
 
The feature licenses available for main cisco asa 5500 models
The feature licenses available for main cisco asa 5500 modelsThe feature licenses available for main cisco asa 5500 models
The feature licenses available for main cisco asa 5500 models
 
How to configure Nagios in Fedora ?
How to configure Nagios in Fedora ?How to configure Nagios in Fedora ?
How to configure Nagios in Fedora ?
 
Ordering guide for cisco isr g2
Ordering guide for cisco isr g2Ordering guide for cisco isr g2
Ordering guide for cisco isr g2
 
How to configure flexible netflow export on cisco routers
How to configure flexible netflow export on cisco routersHow to configure flexible netflow export on cisco routers
How to configure flexible netflow export on cisco routers
 
5. mrtg in nagios1 0
5. mrtg in nagios1 05. mrtg in nagios1 0
5. mrtg in nagios1 0
 
Nagios
NagiosNagios
Nagios
 
BGP Protocol Makes the Internet Work
BGP Protocol Makes the Internet WorkBGP Protocol Makes the Internet Work
BGP Protocol Makes the Internet Work
 
Cisco switch selector layer2 or layer3
Cisco switch selector layer2 or layer3Cisco switch selector layer2 or layer3
Cisco switch selector layer2 or layer3
 
Network protocols
Network protocolsNetwork protocols
Network protocols
 
Computer repair -_a_complete_illustrated_guide_to_pc_hardware
Computer repair -_a_complete_illustrated_guide_to_pc_hardwareComputer repair -_a_complete_illustrated_guide_to_pc_hardware
Computer repair -_a_complete_illustrated_guide_to_pc_hardware
 
Nagios nrpe
Nagios nrpeNagios nrpe
Nagios nrpe
 
Plugging Network Security Holes Using NetFlow
Plugging Network Security Holes Using NetFlowPlugging Network Security Holes Using NetFlow
Plugging Network Security Holes Using NetFlow
 
Licensing on Cisco 2960, 3560X and 3750X...
Licensing on Cisco 2960, 3560X and 3750X...Licensing on Cisco 2960, 3560X and 3750X...
Licensing on Cisco 2960, 3560X and 3750X...
 
How to Configure NetFlow v5 & v9 on Cisco Routers
How to Configure NetFlow v5 & v9 on Cisco RoutersHow to Configure NetFlow v5 & v9 on Cisco Routers
How to Configure NetFlow v5 & v9 on Cisco Routers
 
Central management of network and call services
Central management of network and call servicesCentral management of network and call services
Central management of network and call services
 
Line cards that are available for cisco catalyst 4500 series switches
Line cards that are available for cisco catalyst 4500 series switchesLine cards that are available for cisco catalyst 4500 series switches
Line cards that are available for cisco catalyst 4500 series switches
 

Semelhante a GTU PHP Project Training Guidelines

Looping and switch cases
Looping and switch casesLooping and switch cases
Looping and switch cases
MeoRamos
 
Fundamentalsofprogrammingfinal 121011003536-phpapp02
Fundamentalsofprogrammingfinal 121011003536-phpapp02Fundamentalsofprogrammingfinal 121011003536-phpapp02
Fundamentalsofprogrammingfinal 121011003536-phpapp02
thinesonsing
 
Yeahhhh the final requirement!!!
Yeahhhh the final requirement!!!Yeahhhh the final requirement!!!
Yeahhhh the final requirement!!!
olracoatalub
 

Semelhante a GTU PHP Project Training Guidelines (20)

In-Depth Guide On WordPress Coding Standards For PHP & HTML
In-Depth Guide On WordPress Coding Standards For PHP & HTMLIn-Depth Guide On WordPress Coding Standards For PHP & HTML
In-Depth Guide On WordPress Coding Standards For PHP & HTML
 
Looping and switch cases
Looping and switch casesLooping and switch cases
Looping and switch cases
 
GTU Guidelines for Project on JAVA
GTU Guidelines for Project on JAVAGTU Guidelines for Project on JAVA
GTU Guidelines for Project on JAVA
 
Software Development with PHP & Laravel
Software Development  with PHP & LaravelSoftware Development  with PHP & Laravel
Software Development with PHP & Laravel
 
Functional programming is the most extreme programming
Functional programming is the most extreme programmingFunctional programming is the most extreme programming
Functional programming is the most extreme programming
 
Software Craftmanship - Cours Polytech
Software Craftmanship - Cours PolytechSoftware Craftmanship - Cours Polytech
Software Craftmanship - Cours Polytech
 
Introduction to Software - Coder Forge - John Mulhall
Introduction to Software - Coder Forge - John MulhallIntroduction to Software - Coder Forge - John Mulhall
Introduction to Software - Coder Forge - John Mulhall
 
Javascript best practices
Javascript best practicesJavascript best practices
Javascript best practices
 
Fundamentals of programming final
Fundamentals of programming finalFundamentals of programming final
Fundamentals of programming final
 
Fundamentalsofprogrammingfinal 121011003536-phpapp02
Fundamentalsofprogrammingfinal 121011003536-phpapp02Fundamentalsofprogrammingfinal 121011003536-phpapp02
Fundamentalsofprogrammingfinal 121011003536-phpapp02
 
Why Drupal is Rockstar?
Why Drupal is Rockstar?Why Drupal is Rockstar?
Why Drupal is Rockstar?
 
Standard coding practices
Standard coding practicesStandard coding practices
Standard coding practices
 
Web technologies-course 07.pptx
Web technologies-course 07.pptxWeb technologies-course 07.pptx
Web technologies-course 07.pptx
 
Object Oriented Concepts and Principles
Object Oriented Concepts and PrinciplesObject Oriented Concepts and Principles
Object Oriented Concepts and Principles
 
NamingConvention
NamingConventionNamingConvention
NamingConvention
 
Real World Java Compatibility
Real World Java CompatibilityReal World Java Compatibility
Real World Java Compatibility
 
PowerShell Zero To Hero Workshop!
PowerShell Zero To Hero Workshop!PowerShell Zero To Hero Workshop!
PowerShell Zero To Hero Workshop!
 
What should or not be programmed on the web
What should or not be programmed on the  webWhat should or not be programmed on the  web
What should or not be programmed on the web
 
Yeahhhh the final requirement!!!
Yeahhhh the final requirement!!!Yeahhhh the final requirement!!!
Yeahhhh the final requirement!!!
 
25 Real Life Tips In Ruby on Rails Development
25 Real Life Tips In Ruby on Rails Development25 Real Life Tips In Ruby on Rails Development
25 Real Life Tips In Ruby on Rails Development
 

Mais de TOPS Technologies

Web designing live project training
Web designing live project trainingWeb designing live project training
Web designing live project training
TOPS Technologies
 
08 10-2013 gtu projects - develop final sem gtu project in i phone
08 10-2013 gtu projects - develop final sem gtu project in i phone08 10-2013 gtu projects - develop final sem gtu project in i phone
08 10-2013 gtu projects - develop final sem gtu project in i phone
TOPS Technologies
 

Mais de TOPS Technologies (20)

Learn java objects inheritance-overriding-polymorphism
Learn java objects  inheritance-overriding-polymorphismLearn java objects  inheritance-overriding-polymorphism
Learn java objects inheritance-overriding-polymorphism
 
Surat tops conducted one hour seminar on “corporate basic skills”
Surat tops conducted  one hour seminar on “corporate basic skills”Surat tops conducted  one hour seminar on “corporate basic skills”
Surat tops conducted one hour seminar on “corporate basic skills”
 
Word press interview question and answer tops technologies
Word press interview question and answer   tops technologiesWord press interview question and answer   tops technologies
Word press interview question and answer tops technologies
 
How to install android sdk
How to install android sdkHow to install android sdk
How to install android sdk
 
Software testing and quality assurance
Software testing and quality assuranceSoftware testing and quality assurance
Software testing and quality assurance
 
Basics in software testing
Basics in software testingBasics in software testing
Basics in software testing
 
Learn advanced java programming
Learn advanced java programmingLearn advanced java programming
Learn advanced java programming
 
How to create android applications
How to create android applicationsHow to create android applications
How to create android applications
 
What is ui element in i phone developmetn
What is ui element in i phone developmetnWhat is ui element in i phone developmetn
What is ui element in i phone developmetn
 
How to create android applications
How to create android applicationsHow to create android applications
How to create android applications
 
Java live project training
Java live project trainingJava live project training
Java live project training
 
Software testing live project training
Software testing live project trainingSoftware testing live project training
Software testing live project training
 
Web designing live project training
Web designing live project trainingWeb designing live project training
Web designing live project training
 
Php live project training
Php live project trainingPhp live project training
Php live project training
 
iPhone training in ahmedabad by tops technologies
iPhone training in ahmedabad by tops technologiesiPhone training in ahmedabad by tops technologies
iPhone training in ahmedabad by tops technologies
 
Php training in ahmedabad
Php training in ahmedabadPhp training in ahmedabad
Php training in ahmedabad
 
Java training in ahmedabad
Java training in ahmedabadJava training in ahmedabad
Java training in ahmedabad
 
08 10-2013 gtu projects - develop final sem gtu project in i phone
08 10-2013 gtu projects - develop final sem gtu project in i phone08 10-2013 gtu projects - develop final sem gtu project in i phone
08 10-2013 gtu projects - develop final sem gtu project in i phone
 
GTU Asp.net Project Training Guidelines
GTU Asp.net Project Training GuidelinesGTU Asp.net Project Training Guidelines
GTU Asp.net Project Training Guidelines
 
GTU Guidelines for Project on Android
GTU Guidelines for Project on AndroidGTU Guidelines for Project on Android
GTU Guidelines for Project on Android
 

Último

Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
AnaAcapella
 

Último (20)

2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Spatium Project Simulation student brief
Spatium Project Simulation student briefSpatium Project Simulation student brief
Spatium Project Simulation student brief
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 

GTU PHP Project Training Guidelines

  • 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
  • 6. EDITOR SETTINGS  Tabs v. spaces  Linefeeds 9/11/2013 6 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