-
Notifications
You must be signed in to change notification settings - Fork 3
/
CODING_STANDARD
115 lines (94 loc) · 3.13 KB
/
CODING_STANDARD
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
WineHQ Application Database Coding Practice
/**
* HTML
*/
- Respect html coding standards. The current doctype is HTML 4.01 transitional (http://www.w3.org/TR/REC-html40/)
Try to make your content validate nicely (http://validator.w3.org/)
Avoid using implicitly closed elements eg. <br/> in HTML 4.01 transitional, as it can lead to validation errors.
http://www.w3.org/TR/html401/struct/text.html#edef-BR
/**
* Variables naming
*/
variables that don't come from outside your script (i.e. that aren't fetched from superglobals) should be named this way
(a.k.a hungarian notation):
prefix + var_name
Where prefix is one of:
Scalar types:
i for integers
f for floats
s for strings
sh for html strings
b for booleans
Compound types:
a for arrays
o for objects
Special type:
h for handles
the rest of the variable's name is using camel style
examples:
$aUsers
$iTopicId
$hRecordSet
$sQuery
$hResult
/**
* Functions naming
*/
1)functions name should be declarative and be prefixed with the name of the module (=file) where it is stored (for example image_show_thumbnail())
2)methods (functions inside a class) are named like this: setMyName() (i.e. words separated with an upper case character)
3)normal functions (outside a class) are named like this: query_appdb() (i.e. words separated with an underscore)
/**
* general coding guidelines
*/
1) functions, loops and if's are written this way (look at the way {}'s are lined up):
<?php
function do_foo($sVar)
{
if(isset($sVar))
{
echo "bar";
} else
{
echo "foo";
}
}
?>
2) indentation is made of 4 spaces (no tabs please)
3) line length should be no more than 130 characters, preferably < 80
4) use long php tags (<?php ?>) instead of short ones (<? ?>) as :
a) it could be removed from future versions of php
b) if conflicts with tags like <?xml version=1.0 ?> that are used in xhtml
5) do not use vars that require register_globals to be on as:
a) it is off by default in php 4.1+
b) it is more secure
c) it makes it easier to understand where your vars are coming from (forms, session, etc.)
/**
* comments
*/
1) function, method, header and multiline comments:
/**
* This functions does nothing interesing.
* More comments to come here...
*/
function bar()
{
foo();
}
2) one-line comments
// This is a one line comment
3) always put a single space after the comment mark
4) never use # for commenting as it will become obsolete in the future
/**
* string quotes
*/
There are two different ways to quote strings in PHP - single quotes or double quotes.
The difference is that the parser does variable interpolation in double-quoted strings, but not in single quoted strings.
Because of this, always use single quotes unless the string contains a variable that needs to be parsed.
Also if the string contains a variable put it inside double quotes instead of using 'bla' . $var . 'bla';
To increase readability of the code.
Wrong:
$str = "This is a long string without any variables";
$str = 'This string contains a variable ' . $var . ' enough said.';
Right:
$str = 'This is a long string without any variables';
$str = "This string contains a variable $var enough said";