The following page describes the coding styles adhered to when
contributing to the development of CodeIgniter. There is no requirement
to use these styles in your own CodeIgniter application, though they
are recommended.

Files should be saved with Unicode (UTF-8) encoding. The BOM should
not be used. Unlike UTF-16 and UTF-32, there’s no byte order to
indicate in a UTF-8 encoded file, and the BOM 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).

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.

The PHP closing tag on a PHP document ?> 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 MUST OMIT the PHP closing
tag and end with a single empty line instead.

Class names should always start with an uppercase letter. Multiple words
should be separated with an underscore, and not CamelCased.

INCORRECT:

classsuperclassclassSuperClass

CORRECT:

classSuper_class

classSuper_class{publicfunction__construct(){}}

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. Multiple words should be separated
with an underscore.

The guidelines for variable naming are very similar to those used for
class methods. 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.

INCORRECT:

$j='foo';// 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

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.

DocBlock
style comments preceding class, method, and property declarations so they can be
picked up by IDEs:

Use single line comments within code, leaving a blank line between large
comment blocks and 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);

Use of the || “or” comparison operator is discouraged, as its clarity
on some output devices is low (looking like the number 11, for instance).
&& is preferred over AND but either are acceptable, and a space should
always precede and follow !.

INCORRECT:

if($foo||$bar)if($fooAND$bar)// okay but not recommended for common syntax highlighting applicationsif(!$foo)if(!is_array($foo))

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.

Use the same stringency in returning and checking your own variables.
Use === and !== as necessary.

INCORRECT:

// If 'foo' is at the beginning of the string, strpos will return a 0,// resulting in this conditional evaluating as TRUEif(strpos($str,'foo')==FALSE)

CORRECT:

if(strpos($str,'foo')===FALSE)

INCORRECT:

functionbuild_string($str=""){if($str=="")// uh-oh! What if FALSE or the integer 0 is passed as an argument?{}}

CORRECT:

functionbuild_string($str=""){if($str===""){}}

See also information regarding typecasting,
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”:

Do not leave debugging code in your submissions, even when commented out.
Things such as var_dump(), print_r(), die()/exit() should not be included
in your code unless it serves a specific purpose other than debugging.

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.

CodeIgniter recommends PHP 5.4 or newer to be used, but it should be
compatible with PHP 5.2.4. Your code must either be compatible with this
requirement, provide a suitable fallback, or be an optional feature that
dies quietly without affecting a user’s application.

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.

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.

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.

INCORRECT:

$arr[$foo]='foo';

CORRECT:

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

INCORRECT:

functionfoo($bar){}

CORRECT:

functionfoo($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 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().

Make sure that your dev environment has error reporting enabled
for ALL users, and that display_errors is enabled in the PHP
environment. You can check this setting with:

if(ini_get('display_errors')==1){exit"Enabled";}

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:

ini_set('display_errors',1);

Note

Setting the display_errors
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.

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.

INCORRECT:

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