blob: 5613eabecc24e4afa64e5a3f62c260a46845603f [file] [log] [blame]
Derek Jones619b1222011-10-10 16:26:27 -05001###############
2PHP Style Guide
3###############
4
Derek Jones8ede1a22011-10-05 13:34:52 -05005
Eric Roberts4addd5e2013-02-25 14:14:05 -06006The following page describes the coding styles adhered to when
7contributing to the development of CodeIgniter. There is no requirement
8to use these styles in your own CodeIgniter application, though they
9are recommended.
Derek Jones8ede1a22011-10-05 13:34:52 -050010
11.. contents:: Table of Contents
12
13File Format
14===========
15
16Files should be saved with Unicode (UTF-8) encoding. The BOM should
17*not* be used. Unlike UTF-16 and UTF-32, there's no byte order to
18indicate in a UTF-8 encoded file, and the BOM can have a negative side
19effect in PHP of sending output, preventing the application from being
20able to set its own headers. Unix line endings should be used (LF).
21
22Here is how to apply these settings in some of the more common text
23editors. Instructions for your text editor may vary; check your text
24editor's documentation.
25
26TextMate
27''''''''
28
29#. Open the Application Preferences
30#. Click Advanced, and then the "Saving" tab
31#. In "File Encoding", select "UTF-8 (recommended)"
32#. In "Line Endings", select "LF (recommended)"
33#. *Optional:* Check "Use for existing files as well" if you wish to
34 modify the line endings of files you open to your new preference.
35
36BBEdit
37''''''
38
39#. Open the Application Preferences
40#. Select "Text Encodings" on the left.
41#. In "Default text encoding for new documents", select "Unicode (UTF-8,
42 no BOM)"
43#. *Optional:* In "If file's encoding can't be guessed, use", select
44 "Unicode (UTF-8, no BOM)"
45#. Select "Text Files" on the left.
46#. In "Default line breaks", select "Mac OS X and Unix (LF)"
47
48PHP Closing Tag
49===============
50
51The PHP closing tag on a PHP document **?>** is optional to the PHP
52parser. However, if used, any whitespace following the closing tag,
53whether introduced by the developer, user, or an FTP application, can
54cause unwanted output, PHP errors, or if the latter are suppressed,
55blank pages. For this reason, all PHP files should **OMIT** the closing
56PHP tag, and instead use a comment block to mark the end of file and
Andrey Andreev93f5c5d2012-11-22 13:26:07 +020057its location relative to the application root. This allows you to still
Derek Jones8ede1a22011-10-05 13:34:52 -050058identify a file as being complete and not truncated.
59
Derek Jones129c1812011-10-05 17:15:44 -050060**INCORRECT**::
Derek Jones8ede1a22011-10-05 13:34:52 -050061
Derek Jones129c1812011-10-05 17:15:44 -050062 <?php
63
64 echo "Here's my code!";
65
66 ?>
67
68**CORRECT**::
69
70 <?php
71
72 echo "Here's my code!";
73
Andrey Andreev20292312013-07-22 14:29:10 +030074 /* End of file Myfile.php */
Derek Jones129c1812011-10-05 17:15:44 -050075 /* Location: ./system/modules/mymodule/myfile.php */
Derek Jones8ede1a22011-10-05 13:34:52 -050076
Eric Roberts4addd5e2013-02-25 14:14:05 -060077.. note:: There should be no empty line or newline character(s) following
78 the closing comments. If you happen to see one when
79 submitting a pull request, please check your IDE settings and fix it.
80
Andrey Andreev20292312013-07-22 14:29:10 +030081File Naming
82===========
83
84Class files must be named in a Ucfirst-like manner, while any other file name
85(configurations, views, generic scripts, etc.) should be in all lowercase.
86
87**INCORRECT**::
88
89 somelibrary.php
90 someLibrary.php
91 SOMELIBRARY.php
92 Some_Library.php
93
94 Application_config.php
95 Application_Config.php
96 applicationConfig.php
97
98**CORRECT**::
99
Andrey Andreeve3e2c692013-07-22 16:25:44 +0300100 Somelibrary.php
101 Some_library.php
Andrey Andreev20292312013-07-22 14:29:10 +0300102
103 applicationconfig.php
104 application_config.php
105
106Furthermore, class file names should match the name of the class itself.
107For example, if you have a class named `Myclass`, then its filename must
108be **Myclass.php**.
109
Derek Jones8ede1a22011-10-05 13:34:52 -0500110Class and Method Naming
111=======================
112
113Class names should always start with an uppercase letter. Multiple words
Eric Roberts4addd5e2013-02-25 14:14:05 -0600114should be separated with an underscore, and not CamelCased.
Derek Jones8ede1a22011-10-05 13:34:52 -0500115
Derek Jones129c1812011-10-05 17:15:44 -0500116**INCORRECT**::
Derek Jones8ede1a22011-10-05 13:34:52 -0500117
Derek Jones129c1812011-10-05 17:15:44 -0500118 class superclass
119 class SuperClass
120
121**CORRECT**::
122
123 class Super_class
Derek Jones8ede1a22011-10-05 13:34:52 -0500124
125::
126
Derek Jones129c1812011-10-05 17:15:44 -0500127 class Super_class {
Derek Jones8ede1a22011-10-05 13:34:52 -0500128
Andrey Andreevd8e1ac72012-03-26 22:22:37 +0300129 public function __construct()
Derek Jones129c1812011-10-05 17:15:44 -0500130 {
Derek Jones8ede1a22011-10-05 13:34:52 -0500131
Derek Jones129c1812011-10-05 17:15:44 -0500132 }
133 }
134
Eric Roberts4addd5e2013-02-25 14:14:05 -0600135Class methods should be entirely lowercased and named to clearly
136indicate their function, preferably including a verb. Try to avoid
Eric Robertsd746f262013-02-25 19:55:49 -0600137overly long and verbose names. Multiple words should be separated
138with an underscore.
Derek Jones129c1812011-10-05 17:15:44 -0500139
140**INCORRECT**::
141
142 function fileproperties() // not descriptive and needs underscore separator
143 function fileProperties() // not descriptive and uses CamelCase
144 function getfileproperties() // Better! But still missing underscore separator
145 function getFileProperties() // uses CamelCase
146 function get_the_file_properties_from_the_file() // wordy
147
148**CORRECT**::
149
150 function get_file_properties() // descriptive, underscore separator, and all lowercase letters
Derek Jones8ede1a22011-10-05 13:34:52 -0500151
152Variable Names
153==============
154
Eric Roberts4addd5e2013-02-25 14:14:05 -0600155The guidelines for variable naming are very similar to those used for
156class methods. Variables should contain only lowercase letters,
Derek Jones8ede1a22011-10-05 13:34:52 -0500157use underscore separators, and be reasonably named to indicate their
158purpose and contents. Very short, non-word variables should only be used
159as iterators in for() loops.
160
Derek Jones129c1812011-10-05 17:15:44 -0500161**INCORRECT**::
Derek Jones8ede1a22011-10-05 13:34:52 -0500162
Derek Jones129c1812011-10-05 17:15:44 -0500163 $j = 'foo'; // single letter variables should only be used in for() loops
164 $Str // contains uppercase letters
165 $bufferedText // uses CamelCasing, and could be shortened without losing semantic meaning
166 $groupid // multiple words, needs underscore separator
167 $name_of_last_city_used // too long
168
169**CORRECT**::
170
171 for ($j = 0; $j < 10; $j++)
172 $str
173 $buffer
174 $group_id
175 $last_city
Derek Jones8ede1a22011-10-05 13:34:52 -0500176
177Commenting
178==========
179
180In general, code should be commented prolifically. It not only helps
181describe the flow and intent of the code for less experienced
182programmers, but can prove invaluable when returning to your own code
183months down the line. There is not a required format for comments, but
184the following are recommended.
185
186`DocBlock <http://manual.phpdoc.org/HTMLSmartyConverter/HandS/phpDocumentor/tutorial_phpDocumentor.howto.pkg.html#basics.docblock>`_
Timothy Warrenbb8ae012012-04-20 10:31:51 -0400187style comments preceding class, method, and property declarations so they can be
Derek Jones8ede1a22011-10-05 13:34:52 -0500188picked up by IDEs::
189
Derek Jones129c1812011-10-05 17:15:44 -0500190 /**
191 * Super Class
192 *
193 * @package Package Name
194 * @subpackage Subpackage
195 * @category Category
196 * @author Author Name
197 * @link http://example.com
198 */
199 class Super_class {
Derek Jones8ede1a22011-10-05 13:34:52 -0500200
201::
202
Derek Jones129c1812011-10-05 17:15:44 -0500203 /**
204 * Encodes string for use in XML
205 *
Andrey Andreev16a704c2012-11-09 17:25:00 +0200206 * @param string $str Input string
Derek Jones129c1812011-10-05 17:15:44 -0500207 * @return string
208 */
209 function xml_encode($str)
Andrey Andreev16a704c2012-11-09 17:25:00 +0200210
Timothy Warrenbb8ae012012-04-20 10:31:51 -0400211::
212
213 /**
214 * Data for class manipulation
215 *
216 * @var array
217 */
Andrey Andreev16a704c2012-11-09 17:25:00 +0200218 public $data = array();
Derek Jones8ede1a22011-10-05 13:34:52 -0500219
220Use single line comments within code, leaving a blank line between large
221comment blocks and code.
222
223::
224
Derek Jones129c1812011-10-05 17:15:44 -0500225 // break up the string by newlines
226 $parts = explode("\n", $str);
227
228 // A longer comment that needs to give greater detail on what is
229 // occurring and why can use multiple single-line comments. Try to
230 // keep the width reasonable, around 70 characters is the easiest to
231 // read. Don't hesitate to link to permanent external resources
232 // that may provide greater detail:
233 //
234 // http://example.com/information_about_something/in_particular/
235
236 $parts = $this->foo($parts);
Derek Jones8ede1a22011-10-05 13:34:52 -0500237
238Constants
239=========
240
241Constants follow the same guidelines as do variables, except constants
242should always be fully uppercase. *Always use CodeIgniter constants when
243appropriate, i.e. SLASH, LD, RD, PATH_CACHE, etc.*
244
Derek Jones129c1812011-10-05 17:15:44 -0500245**INCORRECT**::
Derek Jones8ede1a22011-10-05 13:34:52 -0500246
Derek Jones129c1812011-10-05 17:15:44 -0500247 myConstant // missing underscore separator and not fully uppercase
248 N // no single-letter constants
249 S_C_VER // not descriptive
250 $str = str_replace('{foo}', 'bar', $str); // should use LD and RD constants
251
252**CORRECT**::
253
254 MY_CONSTANT
255 NEWLINE
256 SUPER_CLASS_VERSION
257 $str = str_replace(LD.'foo'.RD, 'bar', $str);
Derek Jones8ede1a22011-10-05 13:34:52 -0500258
259TRUE, FALSE, and NULL
260=====================
261
262**TRUE**, **FALSE**, and **NULL** keywords should always be fully
263uppercase.
264
Derek Jones129c1812011-10-05 17:15:44 -0500265**INCORRECT**::
Derek Jones8ede1a22011-10-05 13:34:52 -0500266
Derek Jones129c1812011-10-05 17:15:44 -0500267 if ($foo == true)
268 $bar = false;
269 function foo($bar = null)
270
271**CORRECT**::
272
273 if ($foo == TRUE)
274 $bar = FALSE;
275 function foo($bar = NULL)
Derek Jones8ede1a22011-10-05 13:34:52 -0500276
277Logical Operators
278=================
279
Eric Robertsd746f262013-02-25 19:55:49 -0600280Use of the ``||`` "or" comparison operator is discouraged, as its clarity
Eric Roberts4addd5e2013-02-25 14:14:05 -0600281on some output devices is low (looking like the number 11, for instance).
Eric Robertsd746f262013-02-25 19:55:49 -0600282``&&`` is preferred over ``AND`` but either are acceptable, and a space should
283always precede and follow ``!``.
Derek Jones8ede1a22011-10-05 13:34:52 -0500284
Derek Jones129c1812011-10-05 17:15:44 -0500285**INCORRECT**::
Derek Jones8ede1a22011-10-05 13:34:52 -0500286
Derek Jones129c1812011-10-05 17:15:44 -0500287 if ($foo || $bar)
288 if ($foo AND $bar) // okay but not recommended for common syntax highlighting applications
289 if (!$foo)
290 if (! is_array($foo))
291
292**CORRECT**::
293
294 if ($foo OR $bar)
295 if ($foo && $bar) // recommended
296 if ( ! $foo)
297 if ( ! is_array($foo))
298
Derek Jones8ede1a22011-10-05 13:34:52 -0500299
300Comparing Return Values and Typecasting
301=======================================
302
303Some PHP functions return FALSE on failure, but may also have a valid
304return value of "" or 0, which would evaluate to FALSE in loose
305comparisons. Be explicit by comparing the variable type when using these
306return values in conditionals to ensure the return value is indeed what
307you expect, and not a value that has an equivalent loose-type
308evaluation.
309
310Use the same stringency in returning and checking your own variables.
311Use **===** and **!==** as necessary.
Derek Jones8ede1a22011-10-05 13:34:52 -0500312
Derek Jones129c1812011-10-05 17:15:44 -0500313**INCORRECT**::
Derek Jones8ede1a22011-10-05 13:34:52 -0500314
Derek Jones129c1812011-10-05 17:15:44 -0500315 // If 'foo' is at the beginning of the string, strpos will return a 0,
316 // resulting in this conditional evaluating as TRUE
317 if (strpos($str, 'foo') == FALSE)
Derek Jones8ede1a22011-10-05 13:34:52 -0500318
Derek Jones129c1812011-10-05 17:15:44 -0500319**CORRECT**::
320
321 if (strpos($str, 'foo') === FALSE)
322
323**INCORRECT**::
324
325 function build_string($str = "")
326 {
327 if ($str == "") // uh-oh! What if FALSE or the integer 0 is passed as an argument?
328 {
329
330 }
331 }
332
333**CORRECT**::
334
335 function build_string($str = "")
336 {
337 if ($str === "")
338 {
339
340 }
341 }
Derek Jones8ede1a22011-10-05 13:34:52 -0500342
343
Andrey Andreev16a704c2012-11-09 17:25:00 +0200344See also information regarding `typecasting
345<http://php.net/manual/en/language.types.type-juggling.php#language.types.typecasting>`_,
Derek Jones8ede1a22011-10-05 13:34:52 -0500346which can be quite useful. Typecasting has a slightly different effect
347which may be desirable. When casting a variable as a string, for
348instance, NULL and boolean FALSE variables become empty strings, 0 (and
349other numbers) become strings of digits, and boolean TRUE becomes "1"::
350
351 $str = (string) $str; // cast $str as a string
352
353Debugging Code
354==============
355
Eric Roberts4addd5e2013-02-25 14:14:05 -0600356Do not leave debugging code in your submissions, even when commented out.
Eric Robertsd746f262013-02-25 19:55:49 -0600357Things such as ``var_dump()``, ``print_r()``, ``die()``/``exit()`` should not be included
Eric Roberts4addd5e2013-02-25 14:14:05 -0600358in your code unless it serves a specific purpose other than debugging.
Derek Jones8ede1a22011-10-05 13:34:52 -0500359
360Whitespace in Files
361===================
362
363No whitespace can precede the opening PHP tag or follow the closing PHP
364tag. Output is buffered, so whitespace in your files can cause output to
365begin before CodeIgniter outputs its content, leading to errors and an
Eric Roberts4addd5e2013-02-25 14:14:05 -0600366inability for CodeIgniter to send proper headers.
Derek Jones8ede1a22011-10-05 13:34:52 -0500367
Derek Jones8ede1a22011-10-05 13:34:52 -0500368Compatibility
369=============
370
Eric Roberts4addd5e2013-02-25 14:14:05 -0600371CodeIgniter requires a minimum PHP version of 5.2.4. Your code must either
372be compatible with this minimum requirement, provide a suitable fallback,
373or be an optional feature that dies quietly without affecting a user's
374application.
Derek Jones8ede1a22011-10-05 13:34:52 -0500375
Eric Roberts4addd5e2013-02-25 14:14:05 -0600376Additionally, do not use PHP functions that require non-default libraries
377to be installed unless your code contains an alternative method when the
378function is not available.
Derek Jones129c1812011-10-05 17:15:44 -0500379
Derek Jones8ede1a22011-10-05 13:34:52 -0500380One File per Class
381==================
382
Eric Roberts4addd5e2013-02-25 14:14:05 -0600383Use separate files for each class, unless the classes are *closely related*.
Eric Robertsd746f262013-02-25 19:55:49 -0600384An example of a CodeIgniter file that contains multiple classes is the
385Xmlrpc library file.
Derek Jones8ede1a22011-10-05 13:34:52 -0500386
387Whitespace
388==========
389
390Use tabs for whitespace in your code, not spaces. This may seem like a
391small thing, but using tabs instead of whitespace allows the developer
392looking at your code to have indentation at levels that they prefer and
393customize in whatever application they use. And as a side benefit, it
394results in (slightly) more compact files, storing one tab character
395versus, say, four space characters.
396
397Line Breaks
398===========
399
400Files must be saved with Unix line breaks. This is more of an issue for
401developers who work in Windows, but in any case ensure that your text
402editor is setup to save files with Unix line breaks.
403
404Code Indenting
405==============
406
407Use Allman style indenting. With the exception of Class declarations,
408braces are always placed on a line by themselves, and indented at the
409same level as the control statement that "owns" them.
410
Derek Jones129c1812011-10-05 17:15:44 -0500411**INCORRECT**::
Derek Jones8ede1a22011-10-05 13:34:52 -0500412
Derek Jones129c1812011-10-05 17:15:44 -0500413 function foo($bar) {
414 // ...
415 }
416
417 foreach ($arr as $key => $val) {
418 // ...
419 }
420
421 if ($foo == $bar) {
422 // ...
423 } else {
424 // ...
425 }
426
427 for ($i = 0; $i < 10; $i++)
428 {
429 for ($j = 0; $j < 10; $j++)
430 {
431 // ...
432 }
433 }
Timothy Warren82c83072012-01-26 19:02:05 -0500434
435 try {
436 // ...
437 }
438 catch() {
439 // ...
440 }
Derek Jones129c1812011-10-05 17:15:44 -0500441
442**CORRECT**::
443
444 function foo($bar)
445 {
446 // ...
447 }
448
449 foreach ($arr as $key => $val)
450 {
451 // ...
452 }
453
454 if ($foo == $bar)
455 {
456 // ...
457 }
458 else
459 {
460 // ...
461 }
462
463 for ($i = 0; $i < 10; $i++)
464 {
465 for ($j = 0; $j < 10; $j++)
466 {
467 // ...
468 }
469 }
Timothy Warren82c83072012-01-26 19:02:05 -0500470
471 try
472 {
473 // ...
474 }
475 catch()
476 {
477 // ...
478 }
Derek Jones8ede1a22011-10-05 13:34:52 -0500479
480Bracket and Parenthetic Spacing
481===============================
482
483In general, parenthesis and brackets should not use any additional
484spaces. The exception is that a space should always follow PHP control
485structures that accept arguments with parenthesis (declare, do-while,
486elseif, for, foreach, if, switch, while), to help distinguish them from
487functions and increase readability.
488
Derek Jones129c1812011-10-05 17:15:44 -0500489**INCORRECT**::
Derek Jones8ede1a22011-10-05 13:34:52 -0500490
Derek Jones129c1812011-10-05 17:15:44 -0500491 $arr[ $foo ] = 'foo';
492
493**CORRECT**::
494
495 $arr[$foo] = 'foo'; // no spaces around array keys
496
497**INCORRECT**::
498
499 function foo ( $bar )
500 {
501
502 }
503
504**CORRECT**::
505
506 function foo($bar) // no spaces around parenthesis in function declarations
507 {
508
509 }
510
511**INCORRECT**::
512
513 foreach( $query->result() as $row )
514
515**CORRECT**::
516
517 foreach ($query->result() as $row) // single space following PHP control structures, but not in interior parenthesis
Derek Jones8ede1a22011-10-05 13:34:52 -0500518
519Localized Text
520==============
521
Eric Roberts4addd5e2013-02-25 14:14:05 -0600522CodeIgniter libraries should take advantage of corresponding language files
523whenever possible.
Derek Jones8ede1a22011-10-05 13:34:52 -0500524
Derek Jones129c1812011-10-05 17:15:44 -0500525**INCORRECT**::
Derek Jones8ede1a22011-10-05 13:34:52 -0500526
Derek Jones129c1812011-10-05 17:15:44 -0500527 return "Invalid Selection";
528
529**CORRECT**::
530
531 return $this->lang->line('invalid_selection');
Derek Jones8ede1a22011-10-05 13:34:52 -0500532
533Private Methods and Variables
534=============================
535
Eric Roberts4addd5e2013-02-25 14:14:05 -0600536Methods and variables that are only accessed internally,
Derek Jones8ede1a22011-10-05 13:34:52 -0500537such as utility and helper functions that your public methods use for
538code abstraction, should be prefixed with an underscore.
539
540::
541
Andrey Andreev16a704c2012-11-09 17:25:00 +0200542 public function convert_text()
543 private function _convert_text()
Derek Jones8ede1a22011-10-05 13:34:52 -0500544
545PHP Errors
546==========
547
548Code must run error free and not rely on warnings and notices to be
549hidden to meet this requirement. For instance, never access a variable
Andrey Andreev16a704c2012-11-09 17:25:00 +0200550that you did not set yourself (such as ``$_POST`` array keys) without first
551checking to see that it ``isset()``.
Derek Jones8ede1a22011-10-05 13:34:52 -0500552
Eric Roberts4addd5e2013-02-25 14:14:05 -0600553Make sure that your dev environment has error reporting enabled
Derek Jones8ede1a22011-10-05 13:34:52 -0500554for ALL users, and that display_errors is enabled in the PHP
555environment. You can check this setting with::
556
Derek Jones129c1812011-10-05 17:15:44 -0500557 if (ini_get('display_errors') == 1)
558 {
559 exit "Enabled";
560 }
Derek Jones8ede1a22011-10-05 13:34:52 -0500561
Andrey Andreev16a704c2012-11-09 17:25:00 +0200562On some servers where *display_errors* is disabled, and you do not have
Derek Jones8ede1a22011-10-05 13:34:52 -0500563the ability to change this in the php.ini, you can often enable it with::
564
565 ini_set('display_errors', 1);
566
Andrey Andreev16a704c2012-11-09 17:25:00 +0200567.. note:: Setting the `display_errors
568 <http://php.net/manual/en/ref.errorfunc.php#ini.display-errors>`_
569 setting with ``ini_set()`` at runtime is not identical to having
570 it enabled in the PHP environment. Namely, it will not have any
571 effect if the script has fatal errors.
Derek Jones8ede1a22011-10-05 13:34:52 -0500572
573Short Open Tags
574===============
575
576Always use full PHP opening tags, in case a server does not have
Andrey Andreev16a704c2012-11-09 17:25:00 +0200577*short_open_tag* enabled.
Derek Jones8ede1a22011-10-05 13:34:52 -0500578
Derek Jones129c1812011-10-05 17:15:44 -0500579**INCORRECT**::
Derek Jones8ede1a22011-10-05 13:34:52 -0500580
Derek Jones129c1812011-10-05 17:15:44 -0500581 <? echo $foo; ?>
582
583 <?=$foo?>
584
585**CORRECT**::
586
587 <?php echo $foo; ?>
Derek Jones8ede1a22011-10-05 13:34:52 -0500588
Andrey Andreev16a704c2012-11-09 17:25:00 +0200589.. note:: PHP 5.4 will always have the **<?=** tag available.
590
Derek Jones8ede1a22011-10-05 13:34:52 -0500591One Statement Per Line
592======================
593
594Never combine statements on one line.
595
Derek Jones129c1812011-10-05 17:15:44 -0500596**INCORRECT**::
Derek Jones8ede1a22011-10-05 13:34:52 -0500597
Derek Jones129c1812011-10-05 17:15:44 -0500598 $foo = 'this'; $bar = 'that'; $bat = str_replace($foo, $bar, $bag);
599
600**CORRECT**::
601
602 $foo = 'this';
603 $bar = 'that';
604 $bat = str_replace($foo, $bar, $bag);
Derek Jones8ede1a22011-10-05 13:34:52 -0500605
606Strings
607=======
608
609Always use single quoted strings unless you need variables parsed, and
610in cases where you do need variables parsed, use braces to prevent
611greedy token parsing. You may also use double-quoted strings if the
612string contains single quotes, so you do not have to use escape
613characters.
614
Derek Jones129c1812011-10-05 17:15:44 -0500615**INCORRECT**::
Derek Jones8ede1a22011-10-05 13:34:52 -0500616
Derek Jones129c1812011-10-05 17:15:44 -0500617 "My String" // no variable parsing, so no use for double quotes
618 "My string $foo" // needs braces
619 'SELECT foo FROM bar WHERE baz = \'bag\'' // ugly
620
621**CORRECT**::
622
623 'My String'
624 "My string {$foo}"
625 "SELECT foo FROM bar WHERE baz = 'bag'"
Derek Jones8ede1a22011-10-05 13:34:52 -0500626
627SQL Queries
628===========
629
Andrey Andreev16a704c2012-11-09 17:25:00 +0200630SQL keywords are always capitalized: SELECT, INSERT, UPDATE, WHERE,
Derek Jones8ede1a22011-10-05 13:34:52 -0500631AS, JOIN, ON, IN, etc.
632
633Break up long queries into multiple lines for legibility, preferably
634breaking for each clause.
635
Derek Jones129c1812011-10-05 17:15:44 -0500636**INCORRECT**::
Derek Jones8ede1a22011-10-05 13:34:52 -0500637
Derek Jones129c1812011-10-05 17:15:44 -0500638 // keywords are lowercase and query is too long for
639 // a single line (... indicates continuation of line)
640 $query = $this->db->query("select foo, bar, baz, foofoo, foobar as raboof, foobaz from exp_pre_email_addresses
641 ...where foo != 'oof' and baz != 'zab' order by foobaz limit 5, 100");
642
643**CORRECT**::
644
645 $query = $this->db->query("SELECT foo, bar, baz, foofoo, foobar AS raboof, foobaz
646 FROM exp_pre_email_addresses
647 WHERE foo != 'oof'
648 AND baz != 'zab'
649 ORDER BY foobaz
650 LIMIT 5, 100");
Derek Jones8ede1a22011-10-05 13:34:52 -0500651
652Default Function Arguments
653==========================
654
655Whenever appropriate, provide function argument defaults, which helps
656prevent PHP errors with mistaken calls and provides common fallback
657values which can save a few lines of code. Example::
658
Andrey Andreev16a704c2012-11-09 17:25:00 +0200659 function foo($bar = '', $baz = FALSE)