summaryrefslogtreecommitdiffstats
path: root/user_guide/general/styleguide.html
blob: cdf95d27ece6b7c9c9bba96f9751edb4084f3655 (plain)
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Style Guide : CodeIgniter User Guide</title>

<style type='text/css' media='all'>@import url('../userguide.css');</style>
<link rel='stylesheet' type='text/css' media='all' href='../userguide.css' />

<style type="text/css" media="screen">
	code {
		white-space: pre;
	}
</style>

<script type="text/javascript" src="../nav/nav.js"></script>
<script type="text/javascript" src="../nav/prototype.lite.js"></script>
<script type="text/javascript" src="../nav/moo.fx.js"></script>
<script type="text/javascript" src="../nav/user_guide_menu.js"></script>

<meta http-equiv='expires' content='-1' />
<meta http-equiv= 'pragma' content='no-cache' />
<meta name='robots' content='all' />
<meta name='author' content='ExpressionEngine Dev Team' />
<meta name='description' content='CodeIgniter User Guide' />

</head>
<body>

<!-- START NAVIGATION -->
<div id="nav"><div id="nav_inner"><script type="text/javascript">create_menu('../');</script></div></div>
<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>
<div id="masthead">
<table cellpadding="0" cellspacing="0" border="0" style="width:100%">
<tr>
<td><h1>CodeIgniter User Guide Version 2.2.0</h1></td>
<td id="breadcrumb_right"><a href="../toc.html">Table of Contents Page</a></td>
</tr>
</table>
</div>
<!-- END NAVIGATION -->


<!-- START BREADCRUMB -->
<table cellpadding="0" cellspacing="0" border="0" style="width:100%">
<tr>
<td id="breadcrumb">
<a href="http://codeigniter.com/">CodeIgniter Home</a> &nbsp;&#8250;&nbsp;
<a href="../index.html">User Guide Home</a> &nbsp;&#8250;&nbsp;
Style Guide
</td>
<td id="searchbox"><form method="get" action="http://www.google.com/search"><input type="hidden" name="as_sitesearch" id="as_sitesearch" value="ellislab.com/codeigniter/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>
</tr>
</table>
<!-- END BREADCRUMB -->

<br clear="all" />


<!-- START CONTENT -->
<div id="content">


<h1>General Style and Syntax</h1>

<p>The following page describes the use of coding rules adhered to when developing CodeIgniter.</p>


<h2>Table of Contents</h2>
<ul class="minitoc">
	<li><a href="#file_format">File Format</a></li>
	<li><a href="#php_closing_tag">PHP Closing Tag</a></li>
	<li><a href="#class_and_method_naming">Class and Method Naming</a></li>
	<li><a href="#variable_names">Variable Names</a></li>
	<li><a href="#commenting">Commenting</a></li>
	<li><a href="#constants">Constants</a></li>
	<li><a href="#true_false_and_null">TRUE, FALSE, and NULL</a></li>
	<li><a href="#logical_operators">Logical Operators</a></li>
	<li><a href="#comparing_return_values_and_typecasting">Comparing Return Values and Typecasting</a></li>
	<li><a href="#debugging_code">Debugging Code</a></li>
	<li><a href="#whitespace_in_files">Whitespace in Files</a></li>
	<li><a href="#compatibility">Compatibility</a></li>
	<li><a href="#class_and_file_names_using_common_words">Class and File Names using Common Words</a></li>
	<li><a href="#database_table_names">Database Table Names</a></li>
	<li><a href="#one_file_per_class">One File per Class</a></li>
	<li><a href="#whitespace">Whitespace</a></li>
	<li><a href="#line_breaks">Line Breaks</a></li>
	<li><a href="#code_indenting">Code Indenting</a></li>
	<li><a href="#bracket_spacing">Bracket and Parenthetic Spacing</li>
	<li><a href="#localized_text">Localized Text</a></li>
	<li><a href="#private_methods_and_variables">Private Methods and Variables</a></li>
	<li><a href="#php_errors">PHP Errors</a></li>
	<li><a href="#short_open_tags">Short Open Tags</a></li>
	<li><a href="#one_statement_per_line">One Statement Per Line</a></li>
	<li><a href="#strings">Strings</a></li>
	<li><a href="#sql_queries">SQL Queries</a></li>
	<li><a href="#default_function_arguments">Default Function Arguments</a></li>
</ul>

<li>

		<h2><a name="file_format"></a>File Format</h2>
		<div class="guidelineDetails">
			<p>Files should be saved with Unicode (UTF-8) encoding.  The <abbr title="Byte Order Mark">BOM</abbr>
				should <em>not</em> be used.  Unlike UTF-16 and UTF-32, there's no byte order to indicate in
				a UTF-8 encoded file, and the <abbr title="Byte Order Mark">BOM</abbr> can have a negative side effect in PHP of sending output,
				preventing the application from being able to set its own headers.  Unix line endings should
				be used (LF).</p>

			<p>Here is how to apply these settings in some of the more common text editors.  Instructions for your
				text editor may vary; check your text editor's documentation.</p>

			<h5>TextMate</h5>

			<ol>
				<li>Open the Application Preferences</li>
				<li>Click Advanced, and then the "Saving" tab</li>
				<li>In "File Encoding", select "UTF-8 (recommended)"</li>
				<li>In "Line Endings", select "LF (recommended)"</li>
				<li><em>Optional:</em> Check "Use for existing files as well" if you wish to modify the line
					endings of files you open to your new preference.</li>
			</ol>

			<h5>BBEdit</h5>

			<ol>
				<li>Open the Application Preferences</li>
				<li>Select "Text Encodings" on the left.</li>
				<li>In "Default text encoding for new documents", select "Unicode (UTF-8, no BOM)"</li>
				<li><em>Optional:</em> In "If file's encoding can't be guessed, use", select
					"Unicode (UTF-8, no BOM)"</li>
				<li>Select "Text Files" on the left.</li>
				<li>In "Default line breaks", select "Mac OS X and Unix (LF)"</li>
			</ol>
		</div>

		<h2><a name="php_closing_tag"></a>PHP Closing Tag</h2>
		<div class="guidelineDetails">
			<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
				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
				<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.
				This allows you to still identify a file as being complete and not truncated.</p>
<code><strong>INCORRECT</strong>:
&lt;?php

echo "Here's my code!";

?&gt;

<strong>CORRECT</strong>:
&lt;?php

echo "Here's my code!";

/* End of file myfile.php */
/* Location: ./system/modules/mymodule/myfile.php */
</code>
		</div>


		<h2><a name="class_and_method_naming"></a>Class and Method Naming</h2>
		<div class="guidelineDetails">
			<p>Class names should always start with an uppercase letter.  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>

	<code><strong>INCORRECT</strong>:
class superclass
class SuperClass

<strong>CORRECT</strong>:
class Super_class</code>


	<code>class Super_class {

	function __construct()
	{

	}
}</code>

			<p>Examples of improper and proper method naming:</p>

	<code><strong>INCORRECT</strong>:
function fileproperties()		// not descriptive and needs underscore separator
function fileProperties()		// not descriptive and uses CamelCase
function getfileproperties()		// Better!  But still missing underscore separator
function getFileProperties()		// uses CamelCase
function get_the_file_properties_from_the_file()	// wordy

<strong>CORRECT</strong>:
function get_file_properties()	// descriptive, underscore separator, and all lowercase letters</code>

		</div>


		<h2><a name="variable_names"></a>Variable Names</h2>
		<div class="guidelineDetails">
			<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>
<code><strong>INCORRECT</strong>:
$j = &apos;foo&apos;;		// single letter variables should only be used in for() loops
$Str			// contains uppercase letters
$bufferedText		// uses CamelCasing, and could be shortened without losing semantic meaning
$groupid		// multiple words, needs underscore separator
$name_of_last_city_used	// too long

<strong>CORRECT</strong>:
for ($j = 0; $j &lt; 10; $j++)
$str
$buffer
$group_id
$last_city
</code>
		</div>


		<h2><a name="commenting"></a>Commenting</h2>
		<div class="guidelineDetails">
			<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>

			<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>

<code>/**
 * Super Class
 *
 * @package	Package Name
 * @subpackage	Subpackage
 * @category	Category
 * @author	Author Name
 * @link	http://example.com
 */
class Super_class {</code>

<code>/**
 * Encodes string for use in XML
 *
 * @access	public
 * @param	string
 * @return	string
 */
function xml_encode($str)</code>

			<p>Use single line comments within code, leaving a blank line between large comment blocks and code.</p>

<code>// break up the string by newlines
$parts = explode("\n", $str);

// A longer comment that needs to give greater detail on what is
// occurring and why can use multiple single-line comments.  Try to
// keep the width reasonable, around 70 characters is the easiest to
// read.  Don't hesitate to link to permanent external resources
// that may provide greater detail:
//
// http://example.com/information_about_something/in_particular/

$parts = $this->foo($parts);
</code>
		</div>


		<h2><a name="constants"></a>Constants</h2>
		<div class="guidelineDetails">
			<p>Constants follow the same guidelines as do variables, except constants should always be fully uppercase.  <em>Always use CodeIgniter constants when appropriate, i.e. SLASH, LD, RD, PATH_CACHE, etc.</em></p>
<code><strong>INCORRECT</strong>:
myConstant	// missing underscore separator and not fully uppercase
N		// no single-letter constants
S_C_VER		// not descriptive
$str = str_replace('{foo}', 'bar', $str);	// should use LD and RD constants

<strong>CORRECT</strong>:
MY_CONSTANT
NEWLINE
SUPER_CLASS_VERSION
$str = str_replace(LD.'foo'.RD, 'bar', $str);
</code>
		</div>


		<h2><a name="true_false_and_null"></a>TRUE, FALSE, and NULL</h2>
		<div class="guidelineDetails">
			<p><strong>TRUE</strong>, <strong>FALSE</strong>, and <strong>NULL</strong> keywords should always be fully uppercase.</p>
<code><strong>INCORRECT</strong>:
if ($foo == true)
$bar = false;
function foo($bar = null)

<strong>CORRECT</strong>:
if ($foo == TRUE)
$bar = FALSE;
function foo($bar = NULL)</code>
		</div>



		<h2><a name="logical_operators"></a>Logical Operators</h2>
		<div class="guidelineDetails">
			<p>Use of <strong>||</strong> is discouraged as its clarity on some output devices is low (looking like the number 11 for instance).
				<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>
<code><strong>INCORRECT</strong>:
if ($foo || $bar)
if ($foo AND $bar)  // okay but not recommended for common syntax highlighting applications
if (!$foo)
if (! is_array($foo))

<strong>CORRECT</strong>:
if ($foo OR $bar)
if ($foo && $bar) // recommended
if ( ! $foo)
if ( ! is_array($foo))
</code>
		</div>



		<h2><a name="comparing_return_values_and_typecasting"></a>Comparing Return Values and Typecasting</h2>
		<div class="guidelineDetails">
			<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>
			<p>Use the same stringency in returning and checking your own variables.  Use <strong>===</strong> and <strong>!==</strong> as necessary.

<code><strong>INCORRECT</strong>:
// If 'foo' is at the beginning of the string, strpos will return a 0,
// resulting in this conditional evaluating as TRUE
if (strpos($str, 'foo') == FALSE)

<strong>CORRECT</strong>:
if (strpos($str, 'foo') === FALSE)
</code>

<code><strong>INCORRECT</strong>:
function build_string($str = "")
{
	if ($str == "")	// uh-oh!  What if FALSE or the integer 0 is passed as an argument?
	{

	}
}

<strong>CORRECT</strong>:
function build_string($str = "")
{
	if ($str === "")
	{

	}
}</code>

		<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>

<code>$str = (string) $str;	// cast $str as a string</code>

		</div>


		<h2><a name="debugging_code"></a>Debugging Code</h2>
		<div class="guidelineDetails">
			<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>

<code>// print_r($foo);</code>
		</div>



		<h2><a name="whitespace_in_files"></a>Whitespace in Files</h2>
		<div class="guidelineDetails">
			<p>No whitespace can precede the opening PHP tag or follow the closing PHP tag.  Output is buffered, so whitespace in your files can cause output to begin before CodeIgniter outputs its content, leading to errors and an inability for CodeIgniter to send proper headers.  In the examples below, select the text with your mouse to reveal the incorrect whitespace.</p>

			<p><strong>INCORRECT</strong>:</p>
<code>
&lt;?php
	// ...there is whitespace and a linebreak above the opening PHP tag
	// as well as whitespace after the closing PHP tag
?&gt;
</code>
			<p><strong>CORRECT</strong>:</p>
<code>&lt;?php
	// this sample has no whitespace before or after the opening and closing PHP tags
?&gt;</code>

		</div>


		<h2><a name="compatibility"></a>Compatibility</h2>
		<div class="guidelineDetails">
			<p>Unless specifically mentioned in your add-on's documentation, all code must be compatible with PHP version 5.1+.  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>
		</div>



		<h2><a name="class_and_file_names_using_common_words"></a>Class and File Names using Common Words</h2>
		<div class="guidelineDetails">
			<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>

<code><strong>INCORRECT</strong>:
class Email		pi.email.php
class Xml		ext.xml.php
class Import		mod.import.php

<strong>CORRECT</strong>:
class Pre_email		pi.pre_email.php
class Pre_xml		ext.pre_xml.php
class Pre_import	mod.pre_import.php
</code>
		</div>


		<h2><a name="database_table_names"></a>Database Table Names</h2>
		<div class="guidelineDetails">
			<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 CodeIgniter's database class will automatically convert 'exp_' to what is actually being used.</p>

<code><strong>INCORRECT</strong>:
email_addresses		// missing both prefixes
pre_email_addresses	// missing exp_ prefix
exp_email_addresses	// missing unique prefix

<strong>CORRECT</strong>:
exp_pre_email_addresses
</code>

			<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>
		</div>



		<h2><a name="one_file_per_class"></a>One File per Class</h2>
		<div class="guidelineDetails">
			<p>Use separate files for each class your add-on uses, unless the classes are <em>closely related</em>.  An example of CodeIgniter 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>
		</div>



		<h2><a name="whitespace"></a>Whitespace</h2>
		<div class="guidelineDetails">
			<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>
		</div>



		<h2><a name="line_breaks"></a>Line Breaks</h2>
		<div class="guidelineDetails">
			<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>
		</div>



		<h2><a name="code_indenting"></a>Code Indenting</h2>
		<div class="guidelineDetails">
			<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>

<code><strong>INCORRECT</strong>:
function foo($bar) {
	// ...
}

foreach ($arr as $key => $val) {
	// ...
}

if ($foo == $bar) {
	// ...
} else {
	// ...
}

for ($i = 0; $i &lt; 10; $i++)
	{
	for ($j = 0; $j &lt; 10; $j++)
		{
		// ...
		}
	}

<strong>CORRECT</strong>:
function foo($bar)
{
	// ...
}

foreach ($arr as $key => $val)
{
	// ...
}

if ($foo == $bar)
{
	// ...
}
else
{
	// ...
}

for ($i = 0; $i &lt; 10; $i++)
{
	for ($j = 0; $j &lt; 10; $j++)
	{
		// ...
	}
}</code>
		</div>


	<h2><a name="bracket_spacing"></a>Bracket and Parenthetic Spacing</h2>
		<div class="guidelineDetails">
			<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>

<code>INCORRECT:
$arr[ $foo ] = 'foo';

CORRECT:
$arr[$foo] = 'foo'; // no spaces around array keys


INCORRECT:
function foo ( $bar )
{

}

CORRECT:
function foo($bar) // no spaces around parenthesis in function declarations
{

}


INCORRECT:
foreach( $query->result() as $row )

CORRECT:
foreach ($query->result() as $row) // single space following PHP control structures, but not in interior parenthesis
</code>
		</div>



		<h2><a name="localized_text"></a>Localized Text</h2>
		<div class="guidelineDetails">
			<p>Any text that is output in the control panel should use language variables in your lang file to allow localization.</p>

<code>INCORRECT:
return "Invalid Selection";

CORRECT:
return $this->lang->line('invalid_selection');</code>
		</div>



		<h2><a name="private_methods_and_variables"></a>Private Methods and Variables</h2>
		<div class="guidelineDetails">
			<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>

<code>convert_text()		// public method
_convert_text()		// private method</code>
		</div>



		<h2><a name="php_errors"></a>PHP Errors</h2>
		<div class="guidelineDetails">
			<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>

			<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>

<code>if (ini_get('display_errors') == 1)
{
	exit "Enabled";
}</code>

			<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>

<code>ini_set('display_errors', 1);</code>

			<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>
		</div>



		<h2><a name="short_open_tags"></a>Short Open Tags</h2>
		<div class="guidelineDetails">
			<p>Always use full PHP opening tags, in case a server does not have short_open_tag enabled.</p>

<code><strong>INCORRECT</strong>:
&lt;? echo $foo; ?&gt;

&lt;?=$foo?&gt;

<strong>CORRECT</strong>:
&lt;?php echo $foo; ?&gt;</code>
		</div>



		<h2><a name="one_statement_per_line"></a>One Statement Per Line</h2>
		<div class="guidelineDetails">
			<p>Never combine statements on one line.</p>

<code><strong>INCORRECT</strong>:
$foo = 'this'; $bar = 'that'; $bat = str_replace($foo, $bar, $bag);

<strong>CORRECT</strong>:
$foo = 'this';
$bar = 'that';
$bat = str_replace($foo, $bar, $bag);
</code>
		</div>



		<h2><a name="strings"></a>Strings</h2>
		<div class="guidelineDetails">
			<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>

<code><strong>INCORRECT</strong>:
"My String"					// no variable parsing, so no use for double quotes
"My string $foo"				// needs braces
'SELECT foo FROM bar WHERE baz = \'bag\''	// ugly

<strong>CORRECT</strong>:
'My String'
"My string {$foo}"
"SELECT foo FROM bar WHERE baz = 'bag'"</code>
		</div>



		<h2><a name="sql_queries"></a>SQL Queries</h2>
		<div class="guidelineDetails">
			<p>MySQL keywords are always capitalized: SELECT, INSERT, UPDATE, WHERE, AS, JOIN, ON, IN, etc.</p>

			<p>Break up long queries into multiple lines for legibility, preferably breaking for each clause.</p>

<code><strong>INCORRECT</strong>:
// keywords are lowercase and query is too long for
// a single line (... indicates continuation of line)
$query = $this->db->query("select foo, bar, baz, foofoo, foobar as raboof, foobaz from exp_pre_email_addresses
...where foo != 'oof' and baz != 'zab' order by foobaz limit 5, 100");

<strong>CORRECT</strong>:
$query = $this->db->query("SELECT foo, bar, baz, foofoo, foobar AS raboof, foobaz
				FROM exp_pre_email_addresses
				WHERE foo != 'oof'
				AND baz != 'zab'
				ORDER BY foobaz
				LIMIT 5, 100");</code>
		</div>



		<h2><a name="default_function_arguments"></a>Default Function Arguments</h2>
		<div class="guidelineDetails">
			<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>

<code>function foo($bar = '', $baz = FALSE)</code>
		</div>



</div>



</div>
<!-- END CONTENT -->


<div id="footer">
<p>
Previous Topic:&nbsp;&nbsp;<a href="security.html">Security</a>
&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
<a href="#top">Top of Page</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
<a href="../index.html">User Guide Home</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
Next Topic:&nbsp;&nbsp;<a href="../doc_style/index.html">Writing Documentation</a>
</p>
<p><a href="http://codeigniter.com">CodeIgniter</a> &nbsp;&middot;&nbsp; Copyright &#169; 2006 - 2014 &nbsp;&middot;&nbsp; <a href="http://ellislab.com/">EllisLab, Inc.</a></p>
</div>

</body>
</html>