/
example.php
179 lines (161 loc) · 3.44 KB
/
example.php
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
<?php
/**
* @file
* Example PHP file
*
* File description goes here.
*
* See: guidelines/php.md
*
* Created by: Topsitemakers
* http://www.topsitemakers.com/
*/
/**
* Pay attention to:
* - comments; single line vs double line; end at column 80
* - brackets and new lines in if/for/while/switch statements
* - arrays
* - single quotes for strings
* - double quotes for SQL
* - use underscores for function, class and string names; no camel case
* - leveling the variable/comment values with spaces is allowed
*/
/**
* Function title.
*
* Short description of what it does.
* The param, return, access etc. Additional comment properties can be
* skipped if the function is self explanatory (get_user_name_by_id()) or if
* function is a hook of another documented function in the system (Drupal/WP).
*
* @param string $arg1
* First sample argument.
*
* @param string $arg2
* Second sample argument.
*
* @param string $arg3
* Third sample argument.
*
* @return string
* Sample value.
*/
function sample_function($arg1, $arg2 = NULL, $arg3 = array()) {
/**
* SQL queries.
*/
// Short.
$query = mysql_query("SELECT * FROM table WHERE status = 1");
// Long.
$long_query = mysql_query("SELECT
u.id,
u.name,
u.email
FROM
users u
WHERE
u.created > 123456 AND
u.status = 1
ORDER BY u.id ASC
LIMIT 1");
/**
* Switch.
*/
switch ($arg1) {
case '1':
$result = 'one';
break;
case '2':
$result = 'two';
break;
default:
$result = 'three';
break;
}
/**
* If statement.
*/
if (isset($arg2)) {
$result = 'Arg 2 is present.';
}
else {
$result = 'Arg 2 is not present.';
}
// Shortened.
$result = $arg2 ? 'Arg 2 is present.' : 'Arg 2 is not present.';
/**
* Do-while statements.
*/
$count = 1;
do {
$count++;
} while($count <= 100);
/**
* Basic calculations and concatenating strings.
*/
$a = 1 + 2 + 3 * (10 + 15);
$name = 'John';
$surname = 'Doe';
$full_name = $name . ' ' . $surname;
/**
* Leveling variable names and values with spacing.
*/
$variable_one = 'Value one';
$variable_two = 'Value two';
$variable_three = 'Value three';
// Another sample.
$output = '<ul>';
$output .= '<li>List item</li>';
$output .= '</ul>';
/**
* Foreach.
*/
foreach ($arg3 as $item) {
$items[] = $item;
}
/**
* While.
*/
$i = 0;
while ($i < 10) {
$i++;
}
/**
* Small details.
*/
// Short array.
$short_array = array('one', 'two', 'three');
// Long array.
$long_array = array(
'Sample value one',
'Sample value two',
'Sample value three',
'Sample value four',
'options' => array(
'one' => 'One',
'two' => 'Two',
'three' => 'Three',
),
);
// Use print instead of echo.
print $full_name;
}
/**
* Sample name class.
*/
class Name extends Parent {
/**
* Function description.
*
* @access public
*
* @param string $arg1
* sample argument
*
* @return string
* argument wrapped with two dashes
*/
public function index($arg1) {
return '--' . $arg1 . '--';
}
}