SlideShare a Scribd company logo
1 of 10
TMW
Coding conventions
1. /**
2. * Short description for file
3. *
4. *
5. * @package Codegniter
6. * @created tai.nguyen
7. * @version 1.0
8. * @date 2013.03.04
9. */
10. class Student{
11. ....
12. }
1) File Format
- It' should be “UTF-8, no BOM” (recommended)
- Every file that contains PHP code must have a docblock
at the top of the file.
- Every function must have a docblock at the top
1. /**
2. * @Description Short description for the function
3. * @Created tai.nguyen
4. * @date 2013.03.04
5. */
6. function login(){
7. ....
8. }
- Class names: may only contain alphanumeric characters, the first letter of
each new word must be capitalized.
- Methods:
. Function names may only contain alphanumeric characters.
. Underscores are not permitted.
. Function names must always start with a lowercase letter.
. When a function name consists of more than one word, the first letter of each
new word must be capitalized.
. Each parameter is separated by ',' and one space.
1. function filterInput($usrId, $usrname){
2. ...
3. }
4.
5. function getElementById($inputId){
6. ...
7. }
8.
9. function widgetFactory(){
10. ...
11. }
. Avoid passing huge numbers of parameters to functions. Difficult to
remember the order of parameters. Consider passing an associative array
of named parameters instead.
9. function __construct( $address = '', $user = 0, $by = 0, $reason = '',
10. $timestamp = 0, $auto = 0, $expiry = '', $anonOnly = 0, $createAccount = 0, $enableAutoblock = 0,
11. $hideName = 0, $blockEmail = 0, $allowUsertalk = 0 )
12. {
13. ...
14. }
9. function __construct($params=array() ){
12. echo 'Your address: '. $params['address'];
13. $this->user = $params['user'];
14. ...
15. }
⇊
- Variable:
. Function names may only contain alphanumeric characters.
. Underscores are not permitted.
. Function names must always start with a lowercase letter.
. When a function name consists of more than one word, the first letter of each
new word must be capitalized.
3. $userId = getIdByEmail($email);
4. $userRecommand = getUserRecommand();
. Put spaces on either side of binary operators.
1. // No:
2. $a=$b+$c;
3.
4. // Yes:
5. $a = $b + $c;
. Global variable: start with “g” word
1. global $gSiteName;
. Global variable in a Class: start with underscore “_” characters
1. class Student{
2. var $_id;
3. var $_name;
4. public function __construct() {
5. }
6. }
- When a string is literal (contains no variable substitutions), "single quote"
should always be used to demarcate the string.
- Strings must be concatenated using the "." operator.
String Concatenation
1. $title = 'Vitalify' . ' ' . 'Asia';
- When concatenating strings with the "." operator. If line is so long, You should
break the statement into multiple lines to improve readability.
1. $sqlStr = 'SELECT `id`, `title`, `description` FROM `tbl_news` '
2. .'WHERE `id` NOT IN (1, 2, 3) AND `status`=1 '
3. .'ORDER BY `title` DESC LIMIT 0, 10';
- Short Open Tags. Always use full PHP opening tags,
in case a server does not have short_open_tag enabled.
- Don't start with empty line
1.
2. <?php
3. echo $foo;
4. ?>
1. <?php
2. echo $foo;
3. ?>
- Don't enter empty lines at the end of file.
1. <?php
2 class Student{
3. }
4.
5.
6. ?>
1. <?php
2 class Student{
3. }
4. ?>
<?php echo $foo; ?><? echo $foo; ?>
⇉
⇉
⇉
Others
- Remove all trailing whitespace at the end of your lines(Eclipse PDT provides)
- Set Tab is 4 whitespaces. Preferences > General > Editors > Text Editors
. “Displayed tab width” input “4”
. “Insert spaces for tabs” must be selected.
The End

More Related Content

Similar to Coding convention php

Hello guys please make sure program runs well USING C anyth.pdf
Hello guys please make sure program runs well USING C anyth.pdfHello guys please make sure program runs well USING C anyth.pdf
Hello guys please make sure program runs well USING C anyth.pdf
actioncbe1
 
Coding Standards & Best Practices for iOS/C#
Coding Standards & Best Practices for iOS/C#Coding Standards & Best Practices for iOS/C#
Coding Standards & Best Practices for iOS/C#
Asim Rais Siddiqui
 
4.7 Automate Loading Data with Little to No Duplicates!
4.7 Automate Loading Data with Little to No Duplicates!4.7 Automate Loading Data with Little to No Duplicates!
4.7 Automate Loading Data with Little to No Duplicates!
TargetX
 

Similar to Coding convention php (20)

latest slide
latest slidelatest slide
latest slide
 
testing add
testing addtesting add
testing add
 
Unit 5 xml (1)
Unit 5   xml (1)Unit 5   xml (1)
Unit 5 xml (1)
 
Hello guys please make sure program runs well USING C anyth.pdf
Hello guys please make sure program runs well USING C anyth.pdfHello guys please make sure program runs well USING C anyth.pdf
Hello guys please make sure program runs well USING C anyth.pdf
 
Coding Best Practices
Coding Best PracticesCoding Best Practices
Coding Best Practices
 
Coding Standards & Best Practices for iOS/C#
Coding Standards & Best Practices for iOS/C#Coding Standards & Best Practices for iOS/C#
Coding Standards & Best Practices for iOS/C#
 
Cosc 1436 java programming/tutorialoutlet
Cosc 1436 java programming/tutorialoutletCosc 1436 java programming/tutorialoutlet
Cosc 1436 java programming/tutorialoutlet
 
File Organization & processing Mid term summer 2014 - modelanswer
File Organization & processing Mid term summer 2014 - modelanswerFile Organization & processing Mid term summer 2014 - modelanswer
File Organization & processing Mid term summer 2014 - modelanswer
 
Coding standard
Coding standardCoding standard
Coding standard
 
Fluent Development with FLOW3 1.0
Fluent Development with FLOW3 1.0Fluent Development with FLOW3 1.0
Fluent Development with FLOW3 1.0
 
M.FLORENCE DAYANA WEB DESIGN -Unit 5 XML
M.FLORENCE DAYANA WEB DESIGN -Unit 5   XMLM.FLORENCE DAYANA WEB DESIGN -Unit 5   XML
M.FLORENCE DAYANA WEB DESIGN -Unit 5 XML
 
Java - Interfaces & Packages
Java - Interfaces & PackagesJava - Interfaces & Packages
Java - Interfaces & Packages
 
Pxb For Yapc2008
Pxb For Yapc2008Pxb For Yapc2008
Pxb For Yapc2008
 
C
CC
C
 
Programming in C
Programming in CProgramming in C
Programming in C
 
Module, mixins and file handling in ruby
Module, mixins and file handling in rubyModule, mixins and file handling in ruby
Module, mixins and file handling in ruby
 
4.7 Automate Loading Data with Little to No Duplicates!
4.7 Automate Loading Data with Little to No Duplicates!4.7 Automate Loading Data with Little to No Duplicates!
4.7 Automate Loading Data with Little to No Duplicates!
 
Licão 13 functions
Licão 13 functionsLicão 13 functions
Licão 13 functions
 
Lecture2_MCS4_Evening.pptx
Lecture2_MCS4_Evening.pptxLecture2_MCS4_Evening.pptx
Lecture2_MCS4_Evening.pptx
 
De constructed-module
De constructed-moduleDe constructed-module
De constructed-module
 

Recently uploaded

+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 

Recently uploaded (20)

+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 

Coding convention php

  • 2. 1. /** 2. * Short description for file 3. * 4. * 5. * @package Codegniter 6. * @created tai.nguyen 7. * @version 1.0 8. * @date 2013.03.04 9. */ 10. class Student{ 11. .... 12. } 1) File Format - It' should be “UTF-8, no BOM” (recommended) - Every file that contains PHP code must have a docblock at the top of the file. - Every function must have a docblock at the top 1. /** 2. * @Description Short description for the function 3. * @Created tai.nguyen 4. * @date 2013.03.04 5. */ 6. function login(){ 7. .... 8. }
  • 3. - Class names: may only contain alphanumeric characters, the first letter of each new word must be capitalized. - Methods: . Function names may only contain alphanumeric characters. . Underscores are not permitted. . Function names must always start with a lowercase letter. . When a function name consists of more than one word, the first letter of each new word must be capitalized. . Each parameter is separated by ',' and one space. 1. function filterInput($usrId, $usrname){ 2. ... 3. } 4. 5. function getElementById($inputId){ 6. ... 7. } 8. 9. function widgetFactory(){ 10. ... 11. }
  • 4. . Avoid passing huge numbers of parameters to functions. Difficult to remember the order of parameters. Consider passing an associative array of named parameters instead. 9. function __construct( $address = '', $user = 0, $by = 0, $reason = '', 10. $timestamp = 0, $auto = 0, $expiry = '', $anonOnly = 0, $createAccount = 0, $enableAutoblock = 0, 11. $hideName = 0, $blockEmail = 0, $allowUsertalk = 0 ) 12. { 13. ... 14. } 9. function __construct($params=array() ){ 12. echo 'Your address: '. $params['address']; 13. $this->user = $params['user']; 14. ... 15. } ⇊ - Variable: . Function names may only contain alphanumeric characters. . Underscores are not permitted. . Function names must always start with a lowercase letter. . When a function name consists of more than one word, the first letter of each new word must be capitalized. 3. $userId = getIdByEmail($email); 4. $userRecommand = getUserRecommand();
  • 5. . Put spaces on either side of binary operators. 1. // No: 2. $a=$b+$c; 3. 4. // Yes: 5. $a = $b + $c; . Global variable: start with “g” word 1. global $gSiteName; . Global variable in a Class: start with underscore “_” characters 1. class Student{ 2. var $_id; 3. var $_name; 4. public function __construct() { 5. } 6. }
  • 6. - When a string is literal (contains no variable substitutions), "single quote" should always be used to demarcate the string. - Strings must be concatenated using the "." operator. String Concatenation 1. $title = 'Vitalify' . ' ' . 'Asia'; - When concatenating strings with the "." operator. If line is so long, You should break the statement into multiple lines to improve readability. 1. $sqlStr = 'SELECT `id`, `title`, `description` FROM `tbl_news` ' 2. .'WHERE `id` NOT IN (1, 2, 3) AND `status`=1 ' 3. .'ORDER BY `title` DESC LIMIT 0, 10';
  • 7. - Short Open Tags. Always use full PHP opening tags, in case a server does not have short_open_tag enabled. - Don't start with empty line 1. 2. <?php 3. echo $foo; 4. ?> 1. <?php 2. echo $foo; 3. ?> - Don't enter empty lines at the end of file. 1. <?php 2 class Student{ 3. } 4. 5. 6. ?> 1. <?php 2 class Student{ 3. } 4. ?> <?php echo $foo; ?><? echo $foo; ?> ⇉ ⇉ ⇉
  • 8. Others - Remove all trailing whitespace at the end of your lines(Eclipse PDT provides)
  • 9. - Set Tab is 4 whitespaces. Preferences > General > Editors > Text Editors . “Displayed tab width” input “4” . “Insert spaces for tabs” must be selected.