blob: e3c413717e428350f0237773f1f15988a4573bb1 [file] [log] [blame]
Rick Ellis5a9fd7a2008-08-31 05:58:32 +00001<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
2<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
3<head>
4
5<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
6<title>Security : CodeIgniter User Guide</title>
7
8<style type='text/css' media='all'>@import url('../userguide.css');</style>
9<link rel='stylesheet' type='text/css' media='all' href='../userguide.css' />
10
11<style type="text/css" media="screen">
12 code {
13 white-space: pre;
14 }
15</style>
16
17<script type="text/javascript" src="../nav/nav.js"></script>
18<script type="text/javascript" src="../nav/prototype.lite.js"></script>
19<script type="text/javascript" src="../nav/moo.fx.js"></script>
20<script type="text/javascript" src="../nav/user_guide_menu.js"></script>
21
22<meta http-equiv='expires' content='-1' />
23<meta http-equiv= 'pragma' content='no-cache' />
24<meta name='robots' content='all' />
25<meta name='author' content='ExpressionEngine Dev Team' />
26<meta name='description' content='CodeIgniter User Guide' />
27
28</head>
29<body>
30
31<!-- START NAVIGATION -->
32<div id="nav"><div id="nav_inner"><script type="text/javascript">create_menu('../');</script></div></div>
33<div id="nav2"><a name="top"></a><a href="javascript:void(0);" onclick="myHeight.toggle();"><img src="../images/nav_toggle_darker.jpg" width="154" height="43" border="0" title="Toggle Table of Contents" alt="Toggle Table of Contents" /></a></div>
34<div id="masthead">
35<table cellpadding="0" cellspacing="0" border="0" style="width:100%">
36<tr>
37<td><h1>CodeIgniter User Guide Version 1.7</h1></td>
38<td id="breadcrumb_right"><a href="../toc.html">Table of Contents Page</a></td>
39</tr>
40</table>
41</div>
42<!-- END NAVIGATION -->
43
44
45<!-- START BREADCRUMB -->
46<table cellpadding="0" cellspacing="0" border="0" style="width:100%">
47<tr>
48<td id="breadcrumb">
49<a href="http://codeigniter.com/">CodeIgniter Home</a> &nbsp;&#8250;&nbsp;
50<a href="../index.html">User Guide Home</a> &nbsp;&#8250;&nbsp;
51Security
52</td>
53<td id="searchbox"><form method="get" action="http://www.google.com/search"><input type="hidden" name="as_sitesearch" id="as_sitesearch" value="codeigniter.com/user_guide/" />Search User Guide&nbsp; <input type="text" class="input" style="width:200px;" name="q" id="q" size="31" maxlength="255" value="" />&nbsp;<input type="submit" class="submit" name="sa" value="Go" /></form></td>
54</tr>
55</table>
56<!-- END BREADCRUMB -->
57
58<br clear="all" />
59
60
61<!-- START CONTENT -->
62<div id="content">
63
64
65<h1>General Style and Syntax</h1>
66
67<p>The following page describes the coding rules use adhere to when developing CodeIgniter.</p>
68
69
70<h2>Table of Contents</h2>
71<ul class="minitoc">
72 <li><a href="#php_closing_tag">PHP Closing Tag</a></li>
73 <li><a href="#class_and_method_naming">Class and Method Naming</a></li>
74 <li><a href="#variable_names">Variable Names</a></li>
75 <li><a href="#commenting">Commenting</a></li>
76 <li><a href="#constants">Constants</a></li>
77 <li><a href="#true_false_and_null">TRUE, FALSE, and NULL</a></li>
78 <li><a href="#logical_operators">Logical Operators</a></li>
79 <li><a href="#comparing_return_values_and_typecasting">Comparing Return Values and Typecasting</a></li>
80 <li><a href="#debugging_code">Debugging Code</a></li>
81 <li><a href="#whitespace_in_files">Whitespace in Files</a></li>
82 <li><a href="#compatibility">Compatibility</a></li>
Rick Ellis5a9fd7a2008-08-31 05:58:32 +000083 <li><a href="#class_and_file_names_using_common_words">Class and File Names using Common Words</a></li>
84 <li><a href="#database_table_names">Database Table Names</a></li>
85 <li><a href="#one_file_per_class">One File per Class</a></li>
86 <li><a href="#whitespace">Whitespace</a></li>
87 <li><a href="#line_breaks">Line Breaks</a></li>
88 <li><a href="#code_indenting">Code Indenting</a></li>
89 <li><a href="#bracket_spacing">Bracket and Parenthetic Spacing</li>
90 <li><a href="#localized_text_in_control_panel">Localized Text in Control Panel</a></li>
91 <li><a href="#private_methods_and_variables">Private Methods and Variables</a></li>
92 <li><a href="#php_errors">PHP Errors</a></li>
93 <li><a href="#short_open_tags">Short Open Tags</a></li>
94 <li><a href="#one_statement_per_line">One Statement Per Line</a></li>
95 <li><a href="#strings">Strings</a></li>
96 <li><a href="#sql_queries">SQL Queries</a></li>
97 <li><a href="#default_function_arguments">Default Function Arguments</a></li>
98 <li><a href="#overlapping_tag_parameters">Overlapping Tag Parameters</a></li>
99</ul>
100
101 <h2><a name="php_closing_tag"></a>PHP Closing Tag</h2>
102 <div class="guidelineDetails">
103 <p>The PHP closing tag on a PHP document <strong>?&gt;</strong> is optional to the PHP parser. However, if used, any whitespace following the closing tag, whether introduced
104 by the developer, user, or an FTP application, can cause unwanted output, PHP errors, or if the latter are suppressed, blank pages. For this reason, all PHP files should
105 <strong>OMIT</strong> the closing PHP tag, and instead use a comment block to mark the end of file and it's location relative to the application root.
106 This allows you to still identify a file as being complete and not truncated.</p>
107<code><strong>INCORRECT</strong>:
108&lt;?php
109
110echo "Here's my code!";
111
112?&gt;
113
114<strong>CORRECT</strong>:
115&lt;?php
116
117echo "Here's my code!";
118
119/* End of file myfile.php */
120/* Location: ./system/modules/mymodule/myfile.php */
121</code>
122 </div>
123
124
125 <h2><a name="class_and_method_naming"></a>Class and Method Naming</h2>
126 <div class="guidelineDetails">
127 <p>Class names should always have their first letter uppercase, and the constructor method should match identically. Multiple words should be separated with an underscore, and not CamelCased. All other class methods should be entirely lowercased and named to clearly indicate their function, preferably including a verb. Try to avoid overly long and verbose names.</p>
128
129 <code><strong>INCORRECT</strong>:
130class superclass
131class SuperClass
132
133<strong>CORRECT</strong>:
134class Super_class</code>
135
136 <p>Notice that the Class and constructor methods are identically named and cased:</p>
137
138 <code>class Super_class {
139
140 function Super_class()
141 {
142
143 }
144}</code>
145
146 <p>Examples of improper and proper method naming:</p>
147
148 <code><strong>INCORRECT</strong>:
149function fileproperties() // not descriptive and needs underscore separator
150function fileProperties() // not descriptive and uses CamelCase
151function getfileproperties() // Better! But still missing underscore separator
152function getFileProperties() // uses CamelCase
153function get_the_file_properties_from_the_file() // wordy
154
155<strong>CORRECT</strong>:
156function get_file_properties() // descriptive, underscore separator, and all lowercase letters</code>
157
158 </div>
159
160
161 <h2><a name="variable_names"></a>Variable Names</h2>
162 <div class="guidelineDetails">
163 <p>The guidelines for variable naming is very similar to that used for class methods. Namely, variables should contain only lowercase letters, use underscore separators, and be reasonably named to indicate their purpose and contents. Very short, non-word variables should only be used as iterators in for() loops.</p>
164<code><strong>INCORRECT</strong>:
165$j = &apos;foo&apos;; // single letter variables should only be used in for() loops
166$Str // contains uppercase letters
167$bufferedText // uses CamelCasing, and could be shortened without losing semantic meaning
168$groupid // multiple words, needs underscore separator
169$name_of_last_city_used // too long
170
171<strong>CORRECT</strong>:
172for ($j = 0; $j &lt; 10; $j++)
173$str
174$buffer
175$group_id
176$last_city
177</code>
178 </div>
179
180
181 <h2><a name="commenting"></a>Commenting</h2>
182 <div class="guidelineDetails">
183 <p>In general, code should be commented prolifically. It not only helps describe the flow and intent of the code for less experienced programmers, but can prove invaluable when returning to your own code months down the line. There is not a required format for comments, but the following are recommended.</p>
184
185 <p><a href="http://manual.phpdoc.org/HTMLSmartyConverter/HandS/phpDocumentor/tutorial_phpDocumentor.howto.pkg.html#basics.docblock">DocBlock</a> style comments preceding class and method declarations so they can be picked up by IDEs:</p>
186
187<code>/**
188 * Super Class
189 *
190 * @package Package Name
191 * @subpackage Subpackage
192 * @category Category
193 * @author Author Name
194 * @link http://example.com
195 */
196class Super_class {</code>
197
198<code>/**
199 * Encodes string for use in XML
200 *
201 * @access public
202 * @param string
203 * @return string
204 */
205function xml_encode($str)</code>
206
207 <p>Use single line comments within code, leaving a blank line between large comment blocks and code.</p>
208
209<code>// break up the string by newlines
210$parts = explode("\n", $str);
211
212// A longer comment that needs to give greater detail on what is
213// occurring and why can use multiple single-line comments. Try to
214// keep the width reasonable, around 70 characters is the easiest to
215// read. Don't hesitate to link to permanent external resources
216// that may provide greater detail:
217//
218// http://example.com/information_about_something/in_particular/
219
220$parts = $this->foo($parts);
221</code>
222 </div>
223
224
225 <h2><a name="constants"></a>Constants</h2>
226 <div class="guidelineDetails">
227 <p>Constants follow the same guidelines as do variables, except constants should always be fully uppercase. <em>Always use ExpressionEngine constants when appropriate, i.e. SLASH, LD, RD, PATH_CACHE, etc.</em></p>
228<code><strong>INCORRECT</strong>:
229myConstant // missing underscore separator and not fully uppercase
230N // no single-letter constants
231S_C_VER // not descriptive
232$str = str_replace('{foo}', 'bar', $str); // should use LD and RD constants
233
234<strong>CORRECT</strong>:
235MY_CONSTANT
236NEWLINE
237SUPER_CLASS_VERSION
238$str = str_replace(LD.'foo'.RD, 'bar', $str);
239</code>
240 </div>
241
242
243 <h2><a name="true_false_and_null"></a>TRUE, FALSE, and NULL</h2>
244 <div class="guidelineDetails">
245 <p><strong>TRUE</strong>, <strong>FALSE</strong>, and <strong>NULL</strong> keywords should always be fully uppercase.</p>
246<code><strong>INCORRECT</strong>:
247if ($foo == true)
248$bar = false;
249function foo($bar = null)
250
251<strong>CORRECT</strong>:
252if ($foo == TRUE)
253$bar = FALSE;
254function foo($bar = NULL)</code>
255 </div>
256
257
258
259 <h2><a name="logical_operators"></a>Logical Operators</h2>
260 <div class="guidelineDetails">
261 <p>Use of <strong>||</strong> is discouraged as its clarity on some output devices is low (looking like the number 11 for instance).
262 <strong>&amp;&amp;</strong> is preferred over <strong>AND</strong> but either are acceptable, and a space should always precede and follow <strong>!</strong>.</p>
263<code><strong>INCORRECT</strong>:
264if ($foo || $bar)
265if ($foo AND $bar) // okay but not recommended for common syntax highlighting applications
266if (!$foo)
267if (! is_array($foo))
268
269<strong>CORRECT</strong>:
270if ($foo OR $bar)
271if ($foo && $bar) // recommended
272if ( ! $foo)
273if ( ! is_array($foo))
274</code>
275 </div>
276
277
278
279 <h2><a name="comparing_return_values_and_typecasting"></a>Comparing Return Values and Typecasting</h2>
280 <div class="guidelineDetails">
281 <p>Some PHP functions return FALSE on failure, but may also have a valid return value of "" or 0, which would evaluate to FALSE in loose comparisons. Be explicit by comparing the variable type when using these return values in conditionals to ensure the return value is indeed what you expect, and not a value that has an equivalent loose-type evaluation.</p>
282 <p>Use the same stringency in returning and checking your own variables. Use <strong>===</strong> and <strong>!==</strong> as necessary.
283
284<code><strong>INCORRECT</strong>:
285// If 'foo' is at the beginning of the string, strpos will return a 0,
286// resulting in this conditional evaluating as TRUE
287if (strpos($str, 'foo') == FALSE)
288
289<strong>CORRECT</strong>:
290if (strpos($str, 'foo') === FALSE)
291</code>
292
293<code><strong>INCORRECT</strong>:
294function build_string($str = "")
295{
296 if ($str == "") // uh-oh! What if FALSE or the integer 0 is passed as an argument?
297 {
298
299 }
300}
301
302<strong>CORRECT</strong>:
303function build_string($str = "")
304{
305 if ($str === "")
306 {
307
308 }
309}</code>
310
311 <p>See also information regarding <a href="http://us3.php.net/manual/en/language.types.type-juggling.php#language.types.typecasting">typecasting</a>, which can be quite useful. Typecasting has a slightly different effect which may be desirable. When casting a variable as a string, for instance, NULL and boolean FALSE variables become empty strings, 0 (and other numbers) become strings of digits, and boolean TRUE becomes "1":</p>
312
313<code>$str = (string) $str; // cast $str as a string</code>
314
315 </div>
316
317
318 <h2><a name="debugging_code"></a>Debugging Code</h2>
319 <div class="guidelineDetails">
320 <p>No debugging code can be left in place for submitted add-ons unless it is commented out, i.e. no var_dump(), print_r(), die(), and exit() calls that were used while creating the add-on, unless they are commented out.</p>
321
322<code>// print_r($foo);</code>
323 </div>
324
325
326
327 <h2><a name="whitespace_in_files"></a>Whitespace in Files</h2>
328 <div class="guidelineDetails">
329 <p>No whitespace can precede the opening PHP tag or follow the closing PHP tag. ExpressionEngine output is buffered, so whitespace in your files can cause output to begin before ExpressionEngine outputs its content, leading to errors and an inability for ExpressionEngine to send proper headers. In the examples below, select the text with your mouse to reveal the incorrect whitespace.</p>
330
331 <p><strong>INCORRECT</strong>:</p>
332<code>
333&lt;?php
334 // ...there is whitespace and a linebreak above the opening PHP tag
335 // as well as whitespace after the closing PHP tag
336?&gt;
337</code>
338 <p><strong>CORRECT</strong>:</p>
339<code>&lt;?php
340 // this sample has no whitespace before or after the opening and closing PHP tags
341?&gt;</code>
342
343 </div>
344
345
346 <h2><a name="compatibility"></a>Compatibility</h2>
347 <div class="guidelineDetails">
348 <p>Unless specifically mentioned in your add-on's documentation, all code must be compatible with PHP version 4.3+. Additionally, do not use PHP functions that require non-default libraries to be installed unless your code contains an alternative method when the function is not available, or you implicitly document that your add-on requires said PHP libraries.</p>
349 </div>
Rick Ellis5a9fd7a2008-08-31 05:58:32 +0000350
Rick Ellis5a9fd7a2008-08-31 05:58:32 +0000351
352
353 <h2><a name="class_and_file_names_using_common_words"></a>Class and File Names using Common Words</h2>
354 <div class="guidelineDetails">
355 <p>When your class or filename is a common word, or might quite likely be identically named in another PHP script, provide a unique prefix to help prevent collision. Always realize that your end users may be running other add-ons or third party PHP scripts. Choose a prefix that is unique to your identity as a developer or company.</p>
356
357<code><strong>INCORRECT</strong>:
358class Email pi.email.php
359class Xml ext.xml.php
360class Import mod.import.php
361
362<strong>CORRECT</strong>:
363class Pre_email pi.pre_email.php
364class Pre_xml ext.pre_xml.php
365class Pre_import mod.pre_import.php
366</code>
367 </div>
368
369
370 <h2><a name="database_table_names"></a>Database Table Names</h2>
371 <div class="guidelineDetails">
372 <p>Any tables that your add-on might use must use the 'exp_' prefix, followed by a prefix uniquely identifying you as the developer or company, and then a short descriptive table name. You do not need to be concerned about the database prefix being used on the user's installation, as ExpressionEngine's database class will automatically convert 'exp_' to what is actually being used.</p>
373
374<code><strong>INCORRECT</strong>:
375email_addresses // missing both prefixes
376pre_email_addresses // missing exp_ prefix
377exp_email_addresses // missing unique prefix
378
379<strong>CORRECT</strong>:
380exp_pre_email_addresses
381</code>
382
383 <p class="important"><strong>NOTE:</strong> Be mindful that MySQL has a limit of 64 characters for table names. This should not be an issue as table names that would exceed this would likely have unreasonable names. For instance, the following table name exceeds this limitation by one character. Silly, no? <strong>exp_pre_email_addresses_of_registered_users_in_seattle_washington</strong>
384 </div>
385
386
387
388 <h2><a name="one_file_per_class"></a>One File per Class</h2>
389 <div class="guidelineDetails">
390 <p>Use separate files for each class your add-on uses, unless the classes are <em>closely related</em>. An example of ExpressionEngine files that contains multiple classes is the Database class file, which contains both the DB class and the DB_Cache class, and the Magpie plugin, which contains both the Magpie and Snoopy classes.</p>
391 </div>
392
393
394
395 <h2><a name="whitespace"></a>Whitespace</h2>
396 <div class="guidelineDetails">
397 <p>Use tabs for whitespace in your code, not spaces. This may seem like a small thing, but using tabs instead of whitespace allows the developer looking at your code to have indentation at levels that they prefer and customize in whatever application they use. And as a side benefit, it results in (slightly) more compact files, storing one tab character versus, say, four space characters.</p>
398 </div>
399
400
401
402 <h2><a name="line_breaks"></a>Line Breaks</h2>
403 <div class="guidelineDetails">
404 <p>Files must be saved with Unix line breaks. This is more of an issue for developers who work in Windows, but in any case ensure that your text editor is setup to save files with Unix line breaks.</p>
405 </div>
406
407
408
409 <h2><a name="code_indenting"></a>Code Indenting</h2>
410 <div class="guidelineDetails">
411 <p>Use Allman style indenting. With the exception of Class declarations, braces are always placed on a line by themselves, and indented at the same level as the control statement that "owns" them.</p>
412
413<code><strong>INCORRECT</strong>:
414function foo($bar) {
415 // ...
416}
417
418foreach ($arr as $key => $val) {
419 // ...
420}
421
422if ($foo == $bar) {
423 // ...
424} else {
425 // ...
426}
427
428for ($i = 0; $i &lt; 10; $i++)
429 {
430 for ($j = 0; $j &lt; 10; $j++)
431 {
432 // ...
433 }
434 }
435
436<strong>CORRECT</strong>:
437function foo($bar)
438{
439 // ...
440}
441
442foreach ($arr as $key => $val)
443{
444 // ...
445}
446
447if ($foo == $bar)
448{
449 // ...
450}
451else
452{
453 // ...
454}
455
456for ($i = 0; $i &lt; 10; $i++)
457{
458 for ($j = 0; $j &lt; 10; $j++)
459 {
460 // ...
461 }
462}</code>
463 </div>
464
465
466 <h2><a name="bracket_spacing"></a>Bracket and Parenthetic Spacing</h2>
467 <div class="guidelineDetails">
468 <p>In general, parenthesis and brackets should not use any additional spaces. The exception is that a space should always follow PHP control structures that accept arguments with parenthesis (declare, do-while, elseif, for, foreach, if, switch, while), to help distinguish them from functions and increase readability.</p>
469
470<code>INCORRECT:
471$arr[ $foo ] = 'foo';
472
473CORRECT:
Rick Ellisf06446e2008-09-09 20:36:27 +0000474$arr[$foo] = 'foo'; // no spaces around array keys
Rick Ellis5a9fd7a2008-08-31 05:58:32 +0000475
476
477INCORRECT:
478function foo ( $bar )
479{
480
481}
482
483CORRECT:
Rick Ellisf06446e2008-09-09 20:36:27 +0000484function foo($bar) // no spaces around parenthesis in function declarations
Rick Ellis5a9fd7a2008-08-31 05:58:32 +0000485{
486
487}
488
489
490INCORRECT:
Rick Ellisf06446e2008-09-09 20:36:27 +0000491foreach( $query->result() as $row )
Rick Ellis5a9fd7a2008-08-31 05:58:32 +0000492
493CORRECT:
Rick Ellisf06446e2008-09-09 20:36:27 +0000494foreach ($query->result() as $row) // single space following PHP control structures, but not in interior parenthesis
Rick Ellis5a9fd7a2008-08-31 05:58:32 +0000495</code>
496 </div>
497
498
499
500 <h2><a name="localized_text_in_control_panel"></a>Localized Text in Control Panel</h2>
501 <div class="guidelineDetails">
502 <p>Any text that is output in the control panel should use language variables in your module's lang file to allow localization.</p>
503
504<code>INCORRECT:
505return "Invalid Selection";
506
507CORRECT:
508return $LANG->line('invalid_selection');</code>
509 </div>
510
511
512
513 <h2><a name="private_methods_and_variables"></a>Private Methods and Variables</h2>
514 <div class="guidelineDetails">
515 <p>Methods and variables that are only accessed internally by your class, such as utility and helper functions that your public methods use for code abstraction, should be prefixed with an underscore.</p>
516
517<code>convert_text() // public method
518_convert_text() // private method</code>
519 </div>
520
521
522
523 <h2><a name="php_errors"></a>PHP Errors</h2>
524 <div class="guidelineDetails">
525 <p>Code must run error free and not rely on warnings and notices to be hidden to meet this requirement. For instance, never access a variable that you did not set yourself (such as $_POST array keys) without first checking to see that it isset().</p>
526
527 <p>Make sure that while developing your add-on, error reporting is enabled for ALL users, and that display_errors is enabled in the PHP environment. You can check this setting with:</p>
528
529<code>if (ini_get('display_errors') == 1)
530{
531 exit "Enabled";
532}</code>
533
534 <p>On some servers where display_errors is disabled, and you do not have the ability to change this in the php.ini, you can often enable it with:</p>
535
536<code>ini_set('display_errors', 1);</code>
537
538 <p class="important"><strong>NOTE:</strong> Setting the <a href="http://us.php.net/manual/en/ref.errorfunc.php#ini.display-errors">display_errors</a> setting with ini_set() at runtime is not identical to having it enabled in the PHP environment. Namely, it will not have any effect if the script has fatal errors</p>
539 </div>
540
541
542
543 <h2><a name="short_open_tags"></a>Short Open Tags</h2>
544 <div class="guidelineDetails">
545 <p>Always use full PHP opening tags, in case a server does not have short_open_tag enabled.</p>
546
547<code><strong>INCORRECT</strong>:
548&lt;? echo $foo; ?&gt;
549
550&lt;?=$foo?&gt;
551
552<strong>CORRECT</strong>:
553&lt;?php echo $foo; ?&gt;</code>
554 </div>
555
556
557
558 <h2><a name="one_statement_per_line"></a>One Statement Per Line</h2>
559 <div class="guidelineDetails">
560 <p>Never combine statements on one line.</p>
561
562<code><strong>INCORRECT</strong>:
563$foo = 'this'; $bar = 'that'; $bat = str_replace($foo, $bar, $bag);
564
565<strong>CORRECT</strong>:
566$foo = 'this';
567$bar = 'that';
568$bat = str_replace($foo, $bar, $bag);
569</code>
570 </div>
571
572
573
574 <h2><a name="strings"></a>Strings</h2>
575 <div class="guidelineDetails">
576 <p>Always use single quoted strings unless you need variables parsed, and in cases where you do need variables parsed, use braces to prevent greedy token parsing. You may also use double-quoted strings if the string contains single quotes, so you do not have to use escape characters.</p>
577
578<code><strong>INCORRECT</strong>:
579"My String" // no variable parsing, so no use for double quotes
580"My string $foo" // needs braces
581'SELECT foo FROM bar WHERE baz = \'bag\'' // ugly
582
583<strong>CORRECT</strong>:
584'My String'
585"My string {$foo}"
586"SELECT foo FROM bar WHERE baz = 'bag'"</code>
587 </div>
588
589
590
591 <h2><a name="sql_queries"></a>SQL Queries</h2>
592 <div class="guidelineDetails">
593 <p>MySQL keywords are always capitalized: SELECT, INSERT, UPDATE, WHERE, AS, JOIN, ON, IN, etc.</p>
594
595 <p>Break up long queries into multiple lines for legibility, preferably breaking for each clause.</p>
596
597<code><strong>INCORRECT</strong>:
598// keywords are lowercase and query is too long for
599// a single line (... indicates continuation of line)
Rick Ellisf06446e2008-09-09 20:36:27 +0000600$query = $this->db->query("select foo, bar, baz, foofoo, foobar as raboof, foobaz from exp_pre_email_addresses
Rick Ellis5a9fd7a2008-08-31 05:58:32 +0000601...where foo != 'oof' and baz != 'zab' order by foobaz limit 5, 100");
602
603<strong>CORRECT</strong>:
Rick Ellisf06446e2008-09-09 20:36:27 +0000604$query = $this->db->query("SELECT foo, bar, baz, foofoo, foobar AS raboof, foobaz
605 FROM exp_pre_email_addresses
606 WHERE foo != 'oof'
607 AND baz != 'zab'
608 ORDER BY foobaz
609 LIMIT 5, 100");</code>
Rick Ellis5a9fd7a2008-08-31 05:58:32 +0000610 </div>
611
612
613
614 <h2><a name="default_function_arguments"></a>Default Function Arguments</h2>
615 <div class="guidelineDetails">
616 <p>Whenever appropriate, provide function argument defaults, which helps prevent PHP errors with mistaken calls and provides common fallback values which can save a few lines of code. Example:</p>
617
618<code>function foo($bar = '', $baz = FALSE)</code>
619 </div>
620
621
622
623 <h2><a name="overlapping_tag_parameters"></a>Overlapping Tag Parameters</h2>
624 <div class="guidelineDetails">
625 <p>Avoid multiple tag parameters that have effect on the same thing. For instance, instead of <strong>include=</strong> and <strong>exclude=</strong>, perhaps allow <strong>include=</strong> to handle the parameter alone, with the addition of "not", e.g. <strong>include="not bar"</strong>. This will prevent problems of parameters overlapping or having to worry about which parameter has priority over another.</p>
626 </div>
627
628
629</div>
630
631
632
633</div>
634<!-- END CONTENT -->
635
636
637<div id="footer">
638<p>
639Previous Topic:&nbsp;&nbsp;<a href="security.html">Security</a>
640&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
641<a href="#top">Top of Page</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
642<a href="../index.html">User Guide Home</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
643Next Topic:&nbsp;&nbsp;<a href="../doc_style/index.html">Writing Documentation</a>
644</p>
645<p><a href="http://codeigniter.com">CodeIgniter</a> &nbsp;&middot;&nbsp; Copyright &#169; 2006-2008 &nbsp;&middot;&nbsp; <a href="http://ellislab.com/">Ellislab, Inc.</a></p>
646</div>
647
648</body>
649</html>