{"id":1487,"date":"2015-04-09T15:45:05","date_gmt":"2015-04-09T07:45:05","guid":{"rendered":"http:\/\/boweihe.me\/?p=1487"},"modified":"2015-04-09T15:45:05","modified_gmt":"2015-04-09T07:45:05","slug":"php-style-guide-php%e4%bb%a3%e7%a0%81%e9%a3%8e%e6%a0%bc","status":"publish","type":"post","link":"https:\/\/dayandcarrot.space\/?p=1487","title":{"rendered":"PHP Style Guide &#124; PHP\u4ee3\u7801\u98ce\u683c"},"content":{"rendered":"<p>\u6765\u81ea<a href=\"http:\/\/www.codeigniter.com\/user_guide\/general\/styleguide.html\" target=\"_blank\" rel=\"noopener noreferrer\">http:\/\/www.codeigniter.com\/user_guide\/general\/styleguide.html<\/a>\uff0c\u611f\u89c9\u8fd8\u633a\u6709\u7528\u7684\u3002<\/p>\n<hr \/>\n<p>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.<\/p>\n<div id=\"table-of-contents\" class=\"contents topic\">\n<p class=\"topic-title first\">Table of Contents<\/p>\n<ul class=\"simple\">\n<li><a id=\"id1\" class=\"reference internal\" href=\"http:\/\/www.codeigniter.com\/user_guide\/general\/styleguide.html#php-style-guide\">PHP Style Guide<\/a>\n<ul>\n<li><a id=\"id2\" class=\"reference internal\" href=\"http:\/\/www.codeigniter.com\/user_guide\/general\/styleguide.html#file-format\">File Format<\/a>\n<ul>\n<li><a id=\"id3\" class=\"reference internal\" href=\"http:\/\/www.codeigniter.com\/user_guide\/general\/styleguide.html#textmate\">TextMate<\/a><\/li>\n<li><a id=\"id4\" class=\"reference internal\" href=\"http:\/\/www.codeigniter.com\/user_guide\/general\/styleguide.html#bbedit\">BBEdit<\/a><\/li>\n<\/ul>\n<\/li>\n<li><a id=\"id5\" class=\"reference internal\" href=\"http:\/\/www.codeigniter.com\/user_guide\/general\/styleguide.html#php-closing-tag\">PHP Closing Tag<\/a><\/li>\n<li><a id=\"id6\" class=\"reference internal\" href=\"http:\/\/www.codeigniter.com\/user_guide\/general\/styleguide.html#file-naming\">File Naming<\/a><\/li>\n<li><a id=\"id7\" class=\"reference internal\" href=\"http:\/\/www.codeigniter.com\/user_guide\/general\/styleguide.html#class-and-method-naming\">Class and Method Naming<\/a><\/li>\n<li><a id=\"id8\" class=\"reference internal\" href=\"http:\/\/www.codeigniter.com\/user_guide\/general\/styleguide.html#variable-names\">Variable Names<\/a><\/li>\n<li><a id=\"id9\" class=\"reference internal\" href=\"http:\/\/www.codeigniter.com\/user_guide\/general\/styleguide.html#commenting\">Commenting<\/a><\/li>\n<li><a id=\"id10\" class=\"reference internal\" href=\"http:\/\/www.codeigniter.com\/user_guide\/general\/styleguide.html#constants\">Constants<\/a><\/li>\n<li><a id=\"id11\" class=\"reference internal\" href=\"http:\/\/www.codeigniter.com\/user_guide\/general\/styleguide.html#true-false-and-null\">TRUE, FALSE, and NULL<\/a><\/li>\n<li><a id=\"id12\" class=\"reference internal\" href=\"http:\/\/www.codeigniter.com\/user_guide\/general\/styleguide.html#logical-operators\">Logical Operators<\/a><\/li>\n<li><a id=\"id13\" class=\"reference internal\" href=\"http:\/\/www.codeigniter.com\/user_guide\/general\/styleguide.html#comparing-return-values-and-typecasting\">Comparing Return Values and Typecasting<\/a><\/li>\n<li><a id=\"id14\" class=\"reference internal\" href=\"http:\/\/www.codeigniter.com\/user_guide\/general\/styleguide.html#debugging-code\">Debugging Code<\/a><\/li>\n<li><a id=\"id15\" class=\"reference internal\" href=\"http:\/\/www.codeigniter.com\/user_guide\/general\/styleguide.html#whitespace-in-files\">Whitespace in Files<\/a><\/li>\n<li><a id=\"id16\" class=\"reference internal\" href=\"http:\/\/www.codeigniter.com\/user_guide\/general\/styleguide.html#compatibility\">Compatibility<\/a><\/li>\n<li><a id=\"id17\" class=\"reference internal\" href=\"http:\/\/www.codeigniter.com\/user_guide\/general\/styleguide.html#one-file-per-class\">One File per Class<\/a><\/li>\n<li><a id=\"id18\" class=\"reference internal\" href=\"http:\/\/www.codeigniter.com\/user_guide\/general\/styleguide.html#whitespace\">Whitespace<\/a><\/li>\n<li><a id=\"id19\" class=\"reference internal\" href=\"http:\/\/www.codeigniter.com\/user_guide\/general\/styleguide.html#line-breaks\">Line Breaks<\/a><\/li>\n<li><a id=\"id20\" class=\"reference internal\" href=\"http:\/\/www.codeigniter.com\/user_guide\/general\/styleguide.html#code-indenting\">Code Indenting<\/a><\/li>\n<li><a id=\"id21\" class=\"reference internal\" href=\"http:\/\/www.codeigniter.com\/user_guide\/general\/styleguide.html#bracket-and-parenthetic-spacing\">Bracket and Parenthetic Spacing<\/a><\/li>\n<li><a id=\"id22\" class=\"reference internal\" href=\"http:\/\/www.codeigniter.com\/user_guide\/general\/styleguide.html#localized-text\">Localized Text<\/a><\/li>\n<li><a id=\"id23\" class=\"reference internal\" href=\"http:\/\/www.codeigniter.com\/user_guide\/general\/styleguide.html#private-methods-and-variables\">Private Methods and Variables<\/a><\/li>\n<li><a id=\"id24\" class=\"reference internal\" href=\"http:\/\/www.codeigniter.com\/user_guide\/general\/styleguide.html#php-errors\">PHP Errors<\/a><\/li>\n<li><a id=\"id25\" class=\"reference internal\" href=\"http:\/\/www.codeigniter.com\/user_guide\/general\/styleguide.html#short-open-tags\">Short Open Tags<\/a><\/li>\n<li><a id=\"id26\" class=\"reference internal\" href=\"http:\/\/www.codeigniter.com\/user_guide\/general\/styleguide.html#one-statement-per-line\">One Statement Per Line<\/a><\/li>\n<li><a id=\"id27\" class=\"reference internal\" href=\"http:\/\/www.codeigniter.com\/user_guide\/general\/styleguide.html#strings\">Strings<\/a><\/li>\n<li><a id=\"id28\" class=\"reference internal\" href=\"http:\/\/www.codeigniter.com\/user_guide\/general\/styleguide.html#sql-queries\">SQL Queries<\/a><\/li>\n<li><a id=\"id29\" class=\"reference internal\" href=\"http:\/\/www.codeigniter.com\/user_guide\/general\/styleguide.html#default-function-arguments\">Default Function Arguments<\/a><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/div>\n<div id=\"file-format\" class=\"section\">\n<h2><a class=\"toc-backref\" href=\"http:\/\/www.codeigniter.com\/user_guide\/general\/styleguide.html#id2\">File Format<\/a><a class=\"headerlink\" title=\"Permalink to this headline\" href=\"http:\/\/www.codeigniter.com\/user_guide\/general\/styleguide.html#file-format\">\u00b6<\/a><\/h2>\n<p>Files should be saved with Unicode (UTF-8) encoding. The BOM should <em>not<\/em> be used. Unlike UTF-16 and UTF-32, there\u2019s 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).<br \/>\nHere 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\u2019s documentation.<\/p>\n<div id=\"textmate\" class=\"section\">\n<h3><a class=\"toc-backref\" href=\"http:\/\/www.codeigniter.com\/user_guide\/general\/styleguide.html#id3\">TextMate<\/a><a class=\"headerlink\" title=\"Permalink to this headline\" href=\"http:\/\/www.codeigniter.com\/user_guide\/general\/styleguide.html#textmate\">\u00b6<\/a><\/h3>\n<ol class=\"arabic simple\">\n<li>Open the Application Preferences<\/li>\n<li>Click Advanced, and then the \u201cSaving\u201d tab<\/li>\n<li>In \u201cFile Encoding\u201d, select \u201cUTF-8 (recommended)\u201d<\/li>\n<li>In \u201cLine Endings\u201d, select \u201cLF (recommended)\u201d<\/li>\n<li><em>Optional:<\/em> Check \u201cUse for existing files as well\u201d if you wish to modify the line endings of files you open to your new preference.<\/li>\n<\/ol>\n<\/div>\n<div id=\"bbedit\" class=\"section\">\n<h3><a class=\"toc-backref\" href=\"http:\/\/www.codeigniter.com\/user_guide\/general\/styleguide.html#id4\">BBEdit<\/a><a class=\"headerlink\" title=\"Permalink to this headline\" href=\"http:\/\/www.codeigniter.com\/user_guide\/general\/styleguide.html#bbedit\">\u00b6<\/a><\/h3>\n<ol class=\"arabic simple\">\n<li>Open the Application Preferences<\/li>\n<li>Select \u201cText Encodings\u201d on the left.<\/li>\n<li>In \u201cDefault text encoding for new documents\u201d, select \u201cUnicode (UTF-8, no BOM)\u201d<\/li>\n<li><em>Optional:<\/em> In \u201cIf file\u2019s encoding can\u2019t be guessed, use\u201d, select \u201cUnicode (UTF-8, no BOM)\u201d<\/li>\n<li>Select \u201cText Files\u201d on the left.<\/li>\n<li>In \u201cDefault line breaks\u201d, select \u201cMac OS X and Unix (LF)\u201d<\/li>\n<\/ol>\n<\/div>\n<\/div>\n<div id=\"php-closing-tag\" class=\"section\">\n<h2><a class=\"toc-backref\" href=\"http:\/\/www.codeigniter.com\/user_guide\/general\/styleguide.html#id5\">PHP Closing Tag<\/a><a class=\"headerlink\" title=\"Permalink to this headline\" href=\"http:\/\/www.codeigniter.com\/user_guide\/general\/styleguide.html#php-closing-tag\">\u00b6<\/a><\/h2>\n<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 MUST OMIT the PHP closing tag and end with a single empty line instead.\n<\/div>\n<div id=\"file-naming\" class=\"section\">\n<h2><a class=\"toc-backref\" href=\"http:\/\/www.codeigniter.com\/user_guide\/general\/styleguide.html#id6\">File Naming<\/a><a class=\"headerlink\" title=\"Permalink to this headline\" href=\"http:\/\/www.codeigniter.com\/user_guide\/general\/styleguide.html#file-naming\">\u00b6<\/a><\/h2>\n<p>Class files must be named in a Ucfirst-like manner, while any other file name (configurations, views, generic scripts, etc.) should be in all lowercase.<br \/>\n<strong>INCORRECT<\/strong>:<\/p>\n<div class=\"highlight-ci\">\n<div class=\"highlight\">\n<pre><span class=\"nx\">somelibrary<\/span><span class=\"o\">.<\/span><span class=\"nx\">php<\/span>\n<span class=\"nx\">someLibrary<\/span><span class=\"o\">.<\/span><span class=\"nx\">php<\/span>\n<span class=\"nx\">SOMELIBRARY<\/span><span class=\"o\">.<\/span><span class=\"nx\">php<\/span>\n<span class=\"nx\">Some_Library<\/span><span class=\"o\">.<\/span><span class=\"nx\">php<\/span>\n<span class=\"nx\">Application_config<\/span><span class=\"o\">.<\/span><span class=\"nx\">php<\/span>\n<span class=\"nx\">Application_Config<\/span><span class=\"o\">.<\/span><span class=\"nx\">php<\/span>\n<span class=\"nx\">applicationConfig<\/span><span class=\"o\">.<\/span><span class=\"nx\">php<\/span>\n<\/pre>\n<\/div>\n<\/div>\n<p><strong>CORRECT<\/strong>:<\/p>\n<div class=\"highlight-ci\">\n<div class=\"highlight\">\n<pre><span class=\"nx\">Somelibrary<\/span><span class=\"o\">.<\/span><span class=\"nx\">php<\/span>\n<span class=\"nx\">Some_library<\/span><span class=\"o\">.<\/span><span class=\"nx\">php<\/span>\n<span class=\"nx\">applicationconfig<\/span><span class=\"o\">.<\/span><span class=\"nx\">php<\/span>\n<span class=\"nx\">application_config<\/span><span class=\"o\">.<\/span><span class=\"nx\">php<\/span>\n<\/pre>\n<\/div>\n<\/div>\n<p>Furthermore, class file names should match the name of the class itself. For example, if you have a class named <cite>Myclass<\/cite>, then its filename must be <strong>Myclass.php<\/strong>.\n<\/div>\n<div id=\"class-and-method-naming\" class=\"section\">\n<h2><a class=\"toc-backref\" href=\"http:\/\/www.codeigniter.com\/user_guide\/general\/styleguide.html#id7\">Class and Method Naming<\/a><a class=\"headerlink\" title=\"Permalink to this headline\" href=\"http:\/\/www.codeigniter.com\/user_guide\/general\/styleguide.html#class-and-method-naming\">\u00b6<\/a><\/h2>\n<p>Class names should always start with an uppercase letter. Multiple words should be separated with an underscore, and not CamelCased.<br \/>\n<strong>INCORRECT<\/strong>:<\/p>\n<div class=\"highlight-ci\">\n<div class=\"highlight\">\n<pre><span class=\"k\">class<\/span> <span class=\"nc\">superclass<\/span>\n<span class=\"k\">class<\/span> <span class=\"nc\">SuperClass<\/span>\n<\/pre>\n<\/div>\n<\/div>\n<p><strong>CORRECT<\/strong>:<\/p>\n<div class=\"highlight-ci\">\n<div class=\"highlight\">\n<pre><span class=\"k\">class<\/span> <span class=\"nc\">Super_class<\/span>\n<\/pre>\n<\/div>\n<\/div>\n<div class=\"highlight-ci\">\n<div class=\"highlight\">\n<pre><span class=\"k\">class<\/span> <span class=\"nc\">Super_class<\/span> <span class=\"p\">{<\/span>\n        <span class=\"k\">public<\/span> <span class=\"k\">function<\/span> <span class=\"nf\">__construct<\/span><span class=\"p\">()<\/span>\n        <span class=\"p\">{<\/span>\n        <span class=\"p\">}<\/span>\n<span class=\"p\">}<\/span>\n<\/pre>\n<\/div>\n<\/div>\n<p>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.<br \/>\n<strong>INCORRECT<\/strong>:<\/p>\n<div class=\"highlight-ci\">\n<div class=\"highlight\">\n<pre><span class=\"k\">function<\/span> <span class=\"nf\">fileproperties<\/span><span class=\"p\">()<\/span>               <span class=\"c1\">\/\/ not descriptive and needs underscore separator<\/span>\n<span class=\"k\">function<\/span> <span class=\"nf\">fileProperties<\/span><span class=\"p\">()<\/span>               <span class=\"c1\">\/\/ not descriptive and uses CamelCase<\/span>\n<span class=\"k\">function<\/span> <span class=\"nf\">getfileproperties<\/span><span class=\"p\">()<\/span>            <span class=\"c1\">\/\/ Better!  But still missing underscore separator<\/span>\n<span class=\"k\">function<\/span> <span class=\"nf\">getFileProperties<\/span><span class=\"p\">()<\/span>            <span class=\"c1\">\/\/ uses CamelCase<\/span>\n<span class=\"k\">function<\/span> <span class=\"nf\">get_the_file_properties_from_the_file<\/span><span class=\"p\">()<\/span>        <span class=\"c1\">\/\/ wordy<\/span>\n<\/pre>\n<\/div>\n<\/div>\n<p><strong>CORRECT<\/strong>:<\/p>\n<div class=\"highlight-ci\">\n<div class=\"highlight\">\n<pre><span class=\"k\">function<\/span> <span class=\"nf\">get_file_properties<\/span><span class=\"p\">()<\/span>  <span class=\"c1\">\/\/ descriptive, underscore separator, and all lowercase letters<\/span>\n<\/pre>\n<\/div>\n<\/div>\n<\/div>\n<div id=\"variable-names\" class=\"section\">\n<h2><a class=\"toc-backref\" href=\"http:\/\/www.codeigniter.com\/user_guide\/general\/styleguide.html#id8\">Variable Names<\/a><a class=\"headerlink\" title=\"Permalink to this headline\" href=\"http:\/\/www.codeigniter.com\/user_guide\/general\/styleguide.html#variable-names\">\u00b6<\/a><\/h2>\n<p>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.<br \/>\n<strong>INCORRECT<\/strong>:<\/p>\n<div class=\"highlight-ci\">\n<div class=\"highlight\">\n<pre><span class=\"nv\">$j<\/span> <span class=\"o\">=<\/span> <span class=\"s1\">'foo'<\/span><span class=\"p\">;<\/span>             <span class=\"c1\">\/\/ single letter variables should only be used in for() loops<\/span>\n<span class=\"nv\">$Str<\/span>                    <span class=\"c1\">\/\/ contains uppercase letters<\/span>\n<span class=\"nv\">$bufferedText<\/span>           <span class=\"c1\">\/\/ uses CamelCasing, and could be shortened without losing semantic meaning<\/span>\n<span class=\"nv\">$groupid<\/span>                <span class=\"c1\">\/\/ multiple words, needs underscore separator<\/span>\n<span class=\"nv\">$name_of_last_city_used<\/span> <span class=\"c1\">\/\/ too long<\/span>\n<\/pre>\n<\/div>\n<\/div>\n<p><strong>CORRECT<\/strong>:<\/p>\n<div class=\"highlight-ci\">\n<div class=\"highlight\">\n<pre><span class=\"k\">for<\/span> <span class=\"p\">(<\/span><span class=\"nv\">$j<\/span> <span class=\"o\">=<\/span> <span class=\"mi\">0<\/span><span class=\"p\">;<\/span> <span class=\"nv\">$j<\/span> <span class=\"o\">&lt;<\/span> <span class=\"mi\">10<\/span><span class=\"p\">;<\/span> <span class=\"nv\">$j<\/span><span class=\"o\">++<\/span><span class=\"p\">)<\/span>\n<span class=\"nv\">$str<\/span>\n<span class=\"nv\">$buffer<\/span>\n<span class=\"nv\">$group_id<\/span>\n<span class=\"nv\">$last_city<\/span>\n<\/pre>\n<\/div>\n<\/div>\n<\/div>\n<div id=\"commenting\" class=\"section\">\n<h2><a class=\"toc-backref\" href=\"http:\/\/www.codeigniter.com\/user_guide\/general\/styleguide.html#id9\">Commenting<\/a><a class=\"headerlink\" title=\"Permalink to this headline\" href=\"http:\/\/www.codeigniter.com\/user_guide\/general\/styleguide.html#commenting\">\u00b6<\/a><\/h2>\n<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.<br \/>\n<a class=\"reference external\" href=\"http:\/\/manual.phpdoc.org\/HTMLSmartyConverter\/HandS\/phpDocumentor\/tutorial_phpDocumentor.howto.pkg.html#basics.docblock\">DocBlock<\/a> style comments preceding class, method, and property declarations so they can be picked up by IDEs:<\/p>\n<div class=\"highlight-ci\">\n<div class=\"highlight\">\n<pre><span class=\"sd\">\/**<\/span>\n<span class=\"sd\"> * Super Class<\/span>\n<span class=\"sd\"> *<\/span>\n<span class=\"sd\"> * @package     Package Name<\/span>\n<span class=\"sd\"> * @subpackage  Subpackage<\/span>\n<span class=\"sd\"> * @category    Category<\/span>\n<span class=\"sd\"> * @author      Author Name<\/span>\n<span class=\"sd\"> * @link        http:\/\/example.com<\/span>\n<span class=\"sd\"> *\/<\/span>\n<span class=\"k\">class<\/span> <span class=\"nc\">Super_class<\/span> <span class=\"p\">{<\/span>\n<\/pre>\n<\/div>\n<\/div>\n<div class=\"highlight-ci\">\n<div class=\"highlight\">\n<pre><span class=\"sd\">\/**<\/span>\n<span class=\"sd\"> * Encodes string for use in XML<\/span>\n<span class=\"sd\"> *<\/span>\n<span class=\"sd\"> * @param       string  $str    Input string<\/span>\n<span class=\"sd\"> * @return      string<\/span>\n<span class=\"sd\"> *\/<\/span>\n<span class=\"k\">function<\/span> <span class=\"nf\">xml_encode<\/span><span class=\"p\">(<\/span><span class=\"nv\">$str<\/span><span class=\"p\">)<\/span>\n<\/pre>\n<\/div>\n<\/div>\n<div class=\"highlight-ci\">\n<div class=\"highlight\">\n<pre><span class=\"sd\">\/**<\/span>\n<span class=\"sd\"> * Data for class manipulation<\/span>\n<span class=\"sd\"> *<\/span>\n<span class=\"sd\"> * @var array<\/span>\n<span class=\"sd\"> *\/<\/span>\n<span class=\"k\">public<\/span> <span class=\"nv\">$data<\/span> <span class=\"o\">=<\/span> <span class=\"k\">array<\/span><span class=\"p\">();<\/span>\n<\/pre>\n<\/div>\n<\/div>\n<p>Use single line comments within code, leaving a blank line between large comment blocks and code.<\/p>\n<div class=\"highlight-ci\">\n<div class=\"highlight\">\n<pre><span class=\"c1\">\/\/ break up the string by newlines<\/span>\n<span class=\"nv\">$parts<\/span> <span class=\"o\">=<\/span> <span class=\"nb\">explode<\/span><span class=\"p\">(<\/span><span class=\"s2\">\"<\/span><span class=\"se\">\\n<\/span><span class=\"s2\">\"<\/span><span class=\"p\">,<\/span> <span class=\"nv\">$str<\/span><span class=\"p\">);<\/span>\n<span class=\"c1\">\/\/ A longer comment that needs to give greater detail on what is<\/span>\n<span class=\"c1\">\/\/ occurring and why can use multiple single-line comments.  Try to<\/span>\n<span class=\"c1\">\/\/ keep the width reasonable, around 70 characters is the easiest to<\/span>\n<span class=\"c1\">\/\/ read.  Don't hesitate to link to permanent external resources<\/span>\n<span class=\"c1\">\/\/ that may provide greater detail:<\/span>\n<span class=\"c1\">\/\/<\/span>\n<span class=\"c1\">\/\/ http:\/\/example.com\/information_about_something\/in_particular\/<\/span>\n<span class=\"nv\">$parts<\/span> <span class=\"o\">=<\/span> <span class=\"nv\">$this<\/span><span class=\"o\">-&gt;<\/span><span class=\"na\">foo<\/span><span class=\"p\">(<\/span><span class=\"nv\">$parts<\/span><span class=\"p\">);<\/span>\n<\/pre>\n<\/div>\n<\/div>\n<\/div>\n<div id=\"constants\" class=\"section\">\n<h2><a class=\"toc-backref\" href=\"http:\/\/www.codeigniter.com\/user_guide\/general\/styleguide.html#id10\">Constants<\/a><a class=\"headerlink\" title=\"Permalink to this headline\" href=\"http:\/\/www.codeigniter.com\/user_guide\/general\/styleguide.html#constants\">\u00b6<\/a><\/h2>\n<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><br \/>\n<strong>INCORRECT<\/strong>:<\/p>\n<div class=\"highlight-ci\">\n<div class=\"highlight\">\n<pre><span class=\"nx\">myConstant<\/span>      <span class=\"c1\">\/\/ missing underscore separator and not fully uppercase<\/span>\n<span class=\"nx\">N<\/span>               <span class=\"c1\">\/\/ no single-letter constants<\/span>\n<span class=\"nx\">S_C_VER<\/span>         <span class=\"c1\">\/\/ not descriptive<\/span>\n<span class=\"nv\">$str<\/span> <span class=\"o\">=<\/span> <span class=\"nb\">str_replace<\/span><span class=\"p\">(<\/span><span class=\"s1\">'{foo}'<\/span><span class=\"p\">,<\/span> <span class=\"s1\">'bar'<\/span><span class=\"p\">,<\/span> <span class=\"nv\">$str<\/span><span class=\"p\">);<\/span>       <span class=\"c1\">\/\/ should use LD and RD constants<\/span>\n<\/pre>\n<\/div>\n<\/div>\n<p><strong>CORRECT<\/strong>:<\/p>\n<div class=\"highlight-ci\">\n<div class=\"highlight\">\n<pre><span class=\"nx\">MY_CONSTANT<\/span>\n<span class=\"nx\">NEWLINE<\/span>\n<span class=\"nx\">SUPER_CLASS_VERSION<\/span>\n<span class=\"nv\">$str<\/span> <span class=\"o\">=<\/span> <span class=\"nb\">str_replace<\/span><span class=\"p\">(<\/span><span class=\"nx\">LD<\/span><span class=\"o\">.<\/span><span class=\"s1\">'foo'<\/span><span class=\"o\">.<\/span><span class=\"nx\">RD<\/span><span class=\"p\">,<\/span> <span class=\"s1\">'bar'<\/span><span class=\"p\">,<\/span> <span class=\"nv\">$str<\/span><span class=\"p\">);<\/span>\n<\/pre>\n<\/div>\n<\/div>\n<\/div>\n<div id=\"true-false-and-null\" class=\"section\">\n<h2><a class=\"toc-backref\" href=\"http:\/\/www.codeigniter.com\/user_guide\/general\/styleguide.html#id11\">TRUE, FALSE, and NULL<\/a><a class=\"headerlink\" title=\"Permalink to this headline\" href=\"http:\/\/www.codeigniter.com\/user_guide\/general\/styleguide.html#true-false-and-null\">\u00b6<\/a><\/h2>\n<p><strong>TRUE<\/strong>, <strong>FALSE<\/strong>, and <strong>NULL<\/strong> keywords should always be fully uppercase.<br \/>\n<strong>INCORRECT<\/strong>:<\/p>\n<div class=\"highlight-ci\">\n<div class=\"highlight\">\n<pre><span class=\"k\">if<\/span> <span class=\"p\">(<\/span><span class=\"nv\">$foo<\/span> <span class=\"o\">==<\/span> <span class=\"k\">true<\/span><span class=\"p\">)<\/span>\n<span class=\"nv\">$bar<\/span> <span class=\"o\">=<\/span> <span class=\"k\">false<\/span><span class=\"p\">;<\/span>\n<span class=\"k\">function<\/span> <span class=\"nf\">foo<\/span><span class=\"p\">(<\/span><span class=\"nv\">$bar<\/span> <span class=\"o\">=<\/span> <span class=\"k\">null<\/span><span class=\"p\">)<\/span>\n<\/pre>\n<\/div>\n<\/div>\n<p><strong>CORRECT<\/strong>:<\/p>\n<div class=\"highlight-ci\">\n<div class=\"highlight\">\n<pre><span class=\"k\">if<\/span> <span class=\"p\">(<\/span><span class=\"nv\">$foo<\/span> <span class=\"o\">==<\/span> <span class=\"k\">TRUE<\/span><span class=\"p\">)<\/span>\n<span class=\"nv\">$bar<\/span> <span class=\"o\">=<\/span> <span class=\"k\">FALSE<\/span><span class=\"p\">;<\/span>\n<span class=\"k\">function<\/span> <span class=\"nf\">foo<\/span><span class=\"p\">(<\/span><span class=\"nv\">$bar<\/span> <span class=\"o\">=<\/span> <span class=\"k\">NULL<\/span><span class=\"p\">)<\/span>\n<\/pre>\n<\/div>\n<\/div>\n<\/div>\n<div id=\"logical-operators\" class=\"section\">\n<h2><a class=\"toc-backref\" href=\"http:\/\/www.codeigniter.com\/user_guide\/general\/styleguide.html#id12\">Logical Operators<\/a><a class=\"headerlink\" title=\"Permalink to this headline\" href=\"http:\/\/www.codeigniter.com\/user_guide\/general\/styleguide.html#logical-operators\">\u00b6<\/a><\/h2>\n<p>Use of the <tt class=\"docutils literal\"><span class=\"pre\">||<\/span><\/tt> \u201cor\u201d comparison operator is discouraged, as its clarity on some output devices is low (looking like the number 11, for instance). <tt class=\"docutils literal\"><span class=\"pre\">&amp;&amp;<\/span><\/tt> is preferred over <tt class=\"docutils literal\"><span class=\"pre\">AND<\/span><\/tt> but either are acceptable, and a space should always precede and follow <tt class=\"docutils literal\"><span class=\"pre\">!<\/span><\/tt>.<br \/>\n<strong>INCORRECT<\/strong>:<\/p>\n<div class=\"highlight-ci\">\n<div class=\"highlight\">\n<pre><span class=\"k\">if<\/span> <span class=\"p\">(<\/span><span class=\"nv\">$foo<\/span> <span class=\"o\">||<\/span> <span class=\"nv\">$bar<\/span><span class=\"p\">)<\/span>\n<span class=\"k\">if<\/span> <span class=\"p\">(<\/span><span class=\"nv\">$foo<\/span> <span class=\"k\">AND<\/span> <span class=\"nv\">$bar<\/span><span class=\"p\">)<\/span>  <span class=\"c1\">\/\/ okay but not recommended for common syntax highlighting applications<\/span>\n<span class=\"k\">if<\/span> <span class=\"p\">(<\/span><span class=\"o\">!<\/span><span class=\"nv\">$foo<\/span><span class=\"p\">)<\/span>\n<span class=\"k\">if<\/span> <span class=\"p\">(<\/span><span class=\"o\">!<\/span> <span class=\"nb\">is_array<\/span><span class=\"p\">(<\/span><span class=\"nv\">$foo<\/span><span class=\"p\">))<\/span>\n<\/pre>\n<\/div>\n<\/div>\n<p><strong>CORRECT<\/strong>:<\/p>\n<div class=\"highlight-ci\">\n<div class=\"highlight\">\n<pre><span class=\"k\">if<\/span> <span class=\"p\">(<\/span><span class=\"nv\">$foo<\/span> <span class=\"k\">OR<\/span> <span class=\"nv\">$bar<\/span><span class=\"p\">)<\/span>\n<span class=\"k\">if<\/span> <span class=\"p\">(<\/span><span class=\"nv\">$foo<\/span> <span class=\"o\">&amp;&amp;<\/span> <span class=\"nv\">$bar<\/span><span class=\"p\">)<\/span> <span class=\"c1\">\/\/ recommended<\/span>\n<span class=\"k\">if<\/span> <span class=\"p\">(<\/span> <span class=\"o\">!<\/span> <span class=\"nv\">$foo<\/span><span class=\"p\">)<\/span>\n<span class=\"k\">if<\/span> <span class=\"p\">(<\/span> <span class=\"o\">!<\/span> <span class=\"nb\">is_array<\/span><span class=\"p\">(<\/span><span class=\"nv\">$foo<\/span><span class=\"p\">))<\/span>\n<\/pre>\n<\/div>\n<\/div>\n<\/div>\n<div id=\"comparing-return-values-and-typecasting\" class=\"section\">\n<h2><a class=\"toc-backref\" href=\"http:\/\/www.codeigniter.com\/user_guide\/general\/styleguide.html#id13\">Comparing Return Values and Typecasting<\/a><a class=\"headerlink\" title=\"Permalink to this headline\" href=\"http:\/\/www.codeigniter.com\/user_guide\/general\/styleguide.html#comparing-return-values-and-typecasting\">\u00b6<\/a><\/h2>\n<p>Some PHP functions return FALSE on failure, but may also have a valid return value of \u201c\u201d 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.<br \/>\nUse the same stringency in returning and checking your own variables. Use <strong>===<\/strong> and <strong>!==<\/strong> as necessary.<br \/>\n<strong>INCORRECT<\/strong>:<\/p>\n<div class=\"highlight-ci\">\n<div class=\"highlight\">\n<pre><span class=\"c1\">\/\/ If 'foo' is at the beginning of the string, strpos will return a 0,<\/span>\n<span class=\"c1\">\/\/ resulting in this conditional evaluating as TRUE<\/span>\n<span class=\"k\">if<\/span> <span class=\"p\">(<\/span><span class=\"nb\">strpos<\/span><span class=\"p\">(<\/span><span class=\"nv\">$str<\/span><span class=\"p\">,<\/span> <span class=\"s1\">'foo'<\/span><span class=\"p\">)<\/span> <span class=\"o\">==<\/span> <span class=\"k\">FALSE<\/span><span class=\"p\">)<\/span>\n<\/pre>\n<\/div>\n<\/div>\n<p><strong>CORRECT<\/strong>:<\/p>\n<div class=\"highlight-ci\">\n<div class=\"highlight\">\n<pre><span class=\"k\">if<\/span> <span class=\"p\">(<\/span><span class=\"nb\">strpos<\/span><span class=\"p\">(<\/span><span class=\"nv\">$str<\/span><span class=\"p\">,<\/span> <span class=\"s1\">'foo'<\/span><span class=\"p\">)<\/span> <span class=\"o\">===<\/span> <span class=\"k\">FALSE<\/span><span class=\"p\">)<\/span>\n<\/pre>\n<\/div>\n<\/div>\n<p><strong>INCORRECT<\/strong>:<\/p>\n<div class=\"highlight-ci\">\n<div class=\"highlight\">\n<pre><span class=\"k\">function<\/span> <span class=\"nf\">build_string<\/span><span class=\"p\">(<\/span><span class=\"nv\">$str<\/span> <span class=\"o\">=<\/span> <span class=\"s2\">\"\"<\/span><span class=\"p\">)<\/span>\n<span class=\"p\">{<\/span>\n        <span class=\"k\">if<\/span> <span class=\"p\">(<\/span><span class=\"nv\">$str<\/span> <span class=\"o\">==<\/span> <span class=\"s2\">\"\"<\/span><span class=\"p\">)<\/span> <span class=\"c1\">\/\/ uh-oh!  What if FALSE or the integer 0 is passed as an argument?<\/span>\n        <span class=\"p\">{<\/span>\n        <span class=\"p\">}<\/span>\n<span class=\"p\">}<\/span>\n<\/pre>\n<\/div>\n<\/div>\n<p><strong>CORRECT<\/strong>:<\/p>\n<div class=\"highlight-ci\">\n<div class=\"highlight\">\n<pre><span class=\"k\">function<\/span> <span class=\"nf\">build_string<\/span><span class=\"p\">(<\/span><span class=\"nv\">$str<\/span> <span class=\"o\">=<\/span> <span class=\"s2\">\"\"<\/span><span class=\"p\">)<\/span>\n<span class=\"p\">{<\/span>\n        <span class=\"k\">if<\/span> <span class=\"p\">(<\/span><span class=\"nv\">$str<\/span> <span class=\"o\">===<\/span> <span class=\"s2\">\"\"<\/span><span class=\"p\">)<\/span>\n        <span class=\"p\">{<\/span>\n        <span class=\"p\">}<\/span>\n<span class=\"p\">}<\/span>\n<\/pre>\n<\/div>\n<\/div>\n<p>See also information regarding <a class=\"reference external\" href=\"http:\/\/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 \u201c1\u201d:<\/p>\n<div class=\"highlight-ci\">\n<div class=\"highlight\">\n<pre><span class=\"nv\">$str<\/span> <span class=\"o\">=<\/span> <span class=\"p\">(<\/span><span class=\"nx\">string<\/span><span class=\"p\">)<\/span> <span class=\"nv\">$str<\/span><span class=\"p\">;<\/span> <span class=\"c1\">\/\/ cast $str as a string<\/span>\n<\/pre>\n<\/div>\n<\/div>\n<\/div>\n<div id=\"debugging-code\" class=\"section\">\n<h2><a class=\"toc-backref\" href=\"http:\/\/www.codeigniter.com\/user_guide\/general\/styleguide.html#id14\">Debugging Code<\/a><a class=\"headerlink\" title=\"Permalink to this headline\" href=\"http:\/\/www.codeigniter.com\/user_guide\/general\/styleguide.html#debugging-code\">\u00b6<\/a><\/h2>\n<p>Do not leave debugging code in your submissions, even when commented out. Things such as <tt class=\"docutils literal\"><span class=\"pre\">var_dump()<\/span><\/tt>, <tt class=\"docutils literal\"><span class=\"pre\">print_r()<\/span><\/tt>, <tt class=\"docutils literal\"><span class=\"pre\">die()<\/span><\/tt>\/<tt class=\"docutils literal\"><span class=\"pre\">exit()<\/span><\/tt> should not be included in your code unless it serves a specific purpose other than debugging.\n<\/div>\n<div id=\"whitespace-in-files\" class=\"section\">\n<h2><a class=\"toc-backref\" href=\"http:\/\/www.codeigniter.com\/user_guide\/general\/styleguide.html#id15\">Whitespace in Files<\/a><a class=\"headerlink\" title=\"Permalink to this headline\" href=\"http:\/\/www.codeigniter.com\/user_guide\/general\/styleguide.html#whitespace-in-files\">\u00b6<\/a><\/h2>\n<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.\n<\/p><\/div>\n<div id=\"compatibility\" class=\"section\">\n<h2><a class=\"toc-backref\" href=\"http:\/\/www.codeigniter.com\/user_guide\/general\/styleguide.html#id16\">Compatibility<\/a><a class=\"headerlink\" title=\"Permalink to this headline\" href=\"http:\/\/www.codeigniter.com\/user_guide\/general\/styleguide.html#compatibility\">\u00b6<\/a><\/h2>\n<p>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\u2019s application.<br \/>\nAdditionally, 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.\n<\/p><\/div>\n<div id=\"one-file-per-class\" class=\"section\">\n<h2><a class=\"toc-backref\" href=\"http:\/\/www.codeigniter.com\/user_guide\/general\/styleguide.html#id17\">One File per Class<\/a><a class=\"headerlink\" title=\"Permalink to this headline\" href=\"http:\/\/www.codeigniter.com\/user_guide\/general\/styleguide.html#one-file-per-class\">\u00b6<\/a><\/h2>\n<p>Use separate files for each class, unless the classes are <em>closely related<\/em>. An example of a CodeIgniter file that contains multiple classes is the Xmlrpc library file.\n<\/div>\n<div id=\"whitespace\" class=\"section\">\n<h2><a class=\"toc-backref\" href=\"http:\/\/www.codeigniter.com\/user_guide\/general\/styleguide.html#id18\">Whitespace<\/a><a class=\"headerlink\" title=\"Permalink to this headline\" href=\"http:\/\/www.codeigniter.com\/user_guide\/general\/styleguide.html#whitespace\">\u00b6<\/a><\/h2>\n<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.\n<\/p><\/div>\n<div id=\"line-breaks\" class=\"section\">\n<h2><a class=\"toc-backref\" href=\"http:\/\/www.codeigniter.com\/user_guide\/general\/styleguide.html#id19\">Line Breaks<\/a><a class=\"headerlink\" title=\"Permalink to this headline\" href=\"http:\/\/www.codeigniter.com\/user_guide\/general\/styleguide.html#line-breaks\">\u00b6<\/a><\/h2>\n<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.\n<\/p><\/div>\n<div id=\"code-indenting\" class=\"section\">\n<h2><a class=\"toc-backref\" href=\"http:\/\/www.codeigniter.com\/user_guide\/general\/styleguide.html#id20\">Code Indenting<\/a><a class=\"headerlink\" title=\"Permalink to this headline\" href=\"http:\/\/www.codeigniter.com\/user_guide\/general\/styleguide.html#code-indenting\">\u00b6<\/a><\/h2>\n<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 \u201cowns\u201d them.<br \/>\n<strong>INCORRECT<\/strong>:<\/p>\n<div class=\"highlight-ci\">\n<div class=\"highlight\">\n<pre><span class=\"k\">function<\/span> <span class=\"nf\">foo<\/span><span class=\"p\">(<\/span><span class=\"nv\">$bar<\/span><span class=\"p\">)<\/span> <span class=\"p\">{<\/span>\n        <span class=\"c1\">\/\/ ...<\/span>\n<span class=\"p\">}<\/span>\n<span class=\"k\">foreach<\/span> <span class=\"p\">(<\/span><span class=\"nv\">$arr<\/span> <span class=\"k\">as<\/span> <span class=\"nv\">$key<\/span> <span class=\"o\">=&gt;<\/span> <span class=\"nv\">$val<\/span><span class=\"p\">)<\/span> <span class=\"p\">{<\/span>\n        <span class=\"c1\">\/\/ ...<\/span>\n<span class=\"p\">}<\/span>\n<span class=\"k\">if<\/span> <span class=\"p\">(<\/span><span class=\"nv\">$foo<\/span> <span class=\"o\">==<\/span> <span class=\"nv\">$bar<\/span><span class=\"p\">)<\/span> <span class=\"p\">{<\/span>\n        <span class=\"c1\">\/\/ ...<\/span>\n<span class=\"p\">}<\/span> <span class=\"k\">else<\/span> <span class=\"p\">{<\/span>\n        <span class=\"c1\">\/\/ ...<\/span>\n<span class=\"p\">}<\/span>\n<span class=\"k\">for<\/span> <span class=\"p\">(<\/span><span class=\"nv\">$i<\/span> <span class=\"o\">=<\/span> <span class=\"mi\">0<\/span><span class=\"p\">;<\/span> <span class=\"nv\">$i<\/span> <span class=\"o\">&lt;<\/span> <span class=\"mi\">10<\/span><span class=\"p\">;<\/span> <span class=\"nv\">$i<\/span><span class=\"o\">++<\/span><span class=\"p\">)<\/span>\n        <span class=\"p\">{<\/span>\n        <span class=\"k\">for<\/span> <span class=\"p\">(<\/span><span class=\"nv\">$j<\/span> <span class=\"o\">=<\/span> <span class=\"mi\">0<\/span><span class=\"p\">;<\/span> <span class=\"nv\">$j<\/span> <span class=\"o\">&lt;<\/span> <span class=\"mi\">10<\/span><span class=\"p\">;<\/span> <span class=\"nv\">$j<\/span><span class=\"o\">++<\/span><span class=\"p\">)<\/span>\n                <span class=\"p\">{<\/span>\n                <span class=\"c1\">\/\/ ...<\/span>\n                <span class=\"p\">}<\/span>\n        <span class=\"p\">}<\/span>\n<span class=\"k\">try<\/span> <span class=\"p\">{<\/span>\n        <span class=\"c1\">\/\/ ...<\/span>\n<span class=\"p\">}<\/span>\n<span class=\"k\">catch<\/span><span class=\"p\">()<\/span> <span class=\"p\">{<\/span>\n        <span class=\"c1\">\/\/ ...<\/span>\n<span class=\"p\">}<\/span>\n<\/pre>\n<\/div>\n<\/div>\n<p><strong>CORRECT<\/strong>:<\/p>\n<div class=\"highlight-ci\">\n<div class=\"highlight\">\n<pre><span class=\"k\">function<\/span> <span class=\"nf\">foo<\/span><span class=\"p\">(<\/span><span class=\"nv\">$bar<\/span><span class=\"p\">)<\/span>\n<span class=\"p\">{<\/span>\n        <span class=\"c1\">\/\/ ...<\/span>\n<span class=\"p\">}<\/span>\n<span class=\"k\">foreach<\/span> <span class=\"p\">(<\/span><span class=\"nv\">$arr<\/span> <span class=\"k\">as<\/span> <span class=\"nv\">$key<\/span> <span class=\"o\">=&gt;<\/span> <span class=\"nv\">$val<\/span><span class=\"p\">)<\/span>\n<span class=\"p\">{<\/span>\n        <span class=\"c1\">\/\/ ...<\/span>\n<span class=\"p\">}<\/span>\n<span class=\"k\">if<\/span> <span class=\"p\">(<\/span><span class=\"nv\">$foo<\/span> <span class=\"o\">==<\/span> <span class=\"nv\">$bar<\/span><span class=\"p\">)<\/span>\n<span class=\"p\">{<\/span>\n        <span class=\"c1\">\/\/ ...<\/span>\n<span class=\"p\">}<\/span>\n<span class=\"k\">else<\/span>\n<span class=\"p\">{<\/span>\n        <span class=\"c1\">\/\/ ...<\/span>\n<span class=\"p\">}<\/span>\n<span class=\"k\">for<\/span> <span class=\"p\">(<\/span><span class=\"nv\">$i<\/span> <span class=\"o\">=<\/span> <span class=\"mi\">0<\/span><span class=\"p\">;<\/span> <span class=\"nv\">$i<\/span> <span class=\"o\">&lt;<\/span> <span class=\"mi\">10<\/span><span class=\"p\">;<\/span> <span class=\"nv\">$i<\/span><span class=\"o\">++<\/span><span class=\"p\">)<\/span>\n<span class=\"p\">{<\/span>\n        <span class=\"k\">for<\/span> <span class=\"p\">(<\/span><span class=\"nv\">$j<\/span> <span class=\"o\">=<\/span> <span class=\"mi\">0<\/span><span class=\"p\">;<\/span> <span class=\"nv\">$j<\/span> <span class=\"o\">&lt;<\/span> <span class=\"mi\">10<\/span><span class=\"p\">;<\/span> <span class=\"nv\">$j<\/span><span class=\"o\">++<\/span><span class=\"p\">)<\/span>\n        <span class=\"p\">{<\/span>\n                <span class=\"c1\">\/\/ ...<\/span>\n        <span class=\"p\">}<\/span>\n<span class=\"p\">}<\/span>\n<span class=\"k\">try<\/span>\n<span class=\"p\">{<\/span>\n        <span class=\"c1\">\/\/ ...<\/span>\n<span class=\"p\">}<\/span>\n<span class=\"k\">catch<\/span><span class=\"p\">()<\/span>\n<span class=\"p\">{<\/span>\n        <span class=\"c1\">\/\/ ...<\/span>\n<span class=\"p\">}<\/span>\n<\/pre>\n<\/div>\n<\/div>\n<\/div>\n<div id=\"bracket-and-parenthetic-spacing\" class=\"section\">\n<h2><a class=\"toc-backref\" href=\"http:\/\/www.codeigniter.com\/user_guide\/general\/styleguide.html#id21\">Bracket and Parenthetic Spacing<\/a><a class=\"headerlink\" title=\"Permalink to this headline\" href=\"http:\/\/www.codeigniter.com\/user_guide\/general\/styleguide.html#bracket-and-parenthetic-spacing\">\u00b6<\/a><\/h2>\n<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.<br \/>\n<strong>INCORRECT<\/strong>:<\/p>\n<div class=\"highlight-ci\">\n<div class=\"highlight\">\n<pre><span class=\"nv\">$arr<\/span><span class=\"p\">[<\/span> <span class=\"nv\">$foo<\/span> <span class=\"p\">]<\/span> <span class=\"o\">=<\/span> <span class=\"s1\">'foo'<\/span><span class=\"p\">;<\/span>\n<\/pre>\n<\/div>\n<\/div>\n<p><strong>CORRECT<\/strong>:<\/p>\n<div class=\"highlight-ci\">\n<div class=\"highlight\">\n<pre><span class=\"nv\">$arr<\/span><span class=\"p\">[<\/span><span class=\"nv\">$foo<\/span><span class=\"p\">]<\/span> <span class=\"o\">=<\/span> <span class=\"s1\">'foo'<\/span><span class=\"p\">;<\/span> <span class=\"c1\">\/\/ no spaces around array keys<\/span>\n<\/pre>\n<\/div>\n<\/div>\n<p><strong>INCORRECT<\/strong>:<\/p>\n<div class=\"highlight-ci\">\n<div class=\"highlight\">\n<pre><span class=\"k\">function<\/span> <span class=\"nf\">foo<\/span> <span class=\"p\">(<\/span> <span class=\"nv\">$bar<\/span> <span class=\"p\">)<\/span>\n<span class=\"p\">{<\/span>\n<span class=\"p\">}<\/span>\n<\/pre>\n<\/div>\n<\/div>\n<p><strong>CORRECT<\/strong>:<\/p>\n<div class=\"highlight-ci\">\n<div class=\"highlight\">\n<pre><span class=\"k\">function<\/span> <span class=\"nf\">foo<\/span><span class=\"p\">(<\/span><span class=\"nv\">$bar<\/span><span class=\"p\">)<\/span> <span class=\"c1\">\/\/ no spaces around parenthesis in function declarations<\/span>\n<span class=\"p\">{<\/span>\n<span class=\"p\">}<\/span>\n<\/pre>\n<\/div>\n<\/div>\n<p><strong>INCORRECT<\/strong>:<\/p>\n<div class=\"highlight-ci\">\n<div class=\"highlight\">\n<pre><span class=\"k\">foreach<\/span><span class=\"p\">(<\/span> <span class=\"nv\">$query<\/span><span class=\"o\">-&gt;<\/span><span class=\"na\">result<\/span><span class=\"p\">()<\/span> <span class=\"k\">as<\/span> <span class=\"nv\">$row<\/span> <span class=\"p\">)<\/span>\n<\/pre>\n<\/div>\n<\/div>\n<p><strong>CORRECT<\/strong>:<\/p>\n<div class=\"highlight-ci\">\n<div class=\"highlight\">\n<pre><span class=\"k\">foreach<\/span> <span class=\"p\">(<\/span><span class=\"nv\">$query<\/span><span class=\"o\">-&gt;<\/span><span class=\"na\">result<\/span><span class=\"p\">()<\/span> <span class=\"k\">as<\/span> <span class=\"nv\">$row<\/span><span class=\"p\">)<\/span> <span class=\"c1\">\/\/ single space following PHP control structures, but not in interior parenthesis<\/span>\n<\/pre>\n<\/div>\n<\/div>\n<\/div>\n<div id=\"localized-text\" class=\"section\">\n<h2><a class=\"toc-backref\" href=\"http:\/\/www.codeigniter.com\/user_guide\/general\/styleguide.html#id22\">Localized Text<\/a><a class=\"headerlink\" title=\"Permalink to this headline\" href=\"http:\/\/www.codeigniter.com\/user_guide\/general\/styleguide.html#localized-text\">\u00b6<\/a><\/h2>\n<p>CodeIgniter libraries should take advantage of corresponding language files whenever possible.<br \/>\n<strong>INCORRECT<\/strong>:<\/p>\n<div class=\"highlight-ci\">\n<div class=\"highlight\">\n<pre><span class=\"k\">return<\/span> <span class=\"s2\">\"Invalid Selection\"<\/span><span class=\"p\">;<\/span>\n<\/pre>\n<\/div>\n<\/div>\n<p><strong>CORRECT<\/strong>:<\/p>\n<div class=\"highlight-ci\">\n<div class=\"highlight\">\n<pre><span class=\"k\">return<\/span> <span class=\"nv\">$this<\/span><span class=\"o\">-&gt;<\/span><span class=\"na\">lang<\/span><span class=\"o\">-&gt;<\/span><span class=\"na\">line<\/span><span class=\"p\">(<\/span><span class=\"s1\">'invalid_selection'<\/span><span class=\"p\">);<\/span>\n<\/pre>\n<\/div>\n<\/div>\n<\/div>\n<div id=\"private-methods-and-variables\" class=\"section\">\n<h2><a class=\"toc-backref\" href=\"http:\/\/www.codeigniter.com\/user_guide\/general\/styleguide.html#id23\">Private Methods and Variables<\/a><a class=\"headerlink\" title=\"Permalink to this headline\" href=\"http:\/\/www.codeigniter.com\/user_guide\/general\/styleguide.html#private-methods-and-variables\">\u00b6<\/a><\/h2>\n<p>Methods and variables that are only accessed internally, such as utility and helper functions that your public methods use for code abstraction, should be prefixed with an underscore.<\/p>\n<div class=\"highlight-ci\">\n<div class=\"highlight\">\n<pre><span class=\"k\">public<\/span> <span class=\"k\">function<\/span> <span class=\"nf\">convert_text<\/span><span class=\"p\">()<\/span>\n<span class=\"k\">private<\/span> <span class=\"k\">function<\/span> <span class=\"nf\">_convert_text<\/span><span class=\"p\">()<\/span>\n<\/pre>\n<\/div>\n<\/div>\n<\/div>\n<div id=\"php-errors\" class=\"section\">\n<h2><a class=\"toc-backref\" href=\"http:\/\/www.codeigniter.com\/user_guide\/general\/styleguide.html#id24\">PHP Errors<\/a><a class=\"headerlink\" title=\"Permalink to this headline\" href=\"http:\/\/www.codeigniter.com\/user_guide\/general\/styleguide.html#php-errors\">\u00b6<\/a><\/h2>\n<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 <tt class=\"docutils literal\"><span class=\"pre\">$_POST<\/span><\/tt> array keys) without first checking to see that it <tt class=\"docutils literal\"><span class=\"pre\">isset()<\/span><\/tt>.<br \/>\nMake 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:<\/p>\n<div class=\"highlight-ci\">\n<div class=\"highlight\">\n<pre><span class=\"k\">if<\/span> <span class=\"p\">(<\/span><span class=\"nb\">ini_get<\/span><span class=\"p\">(<\/span><span class=\"s1\">'display_errors'<\/span><span class=\"p\">)<\/span> <span class=\"o\">==<\/span> <span class=\"mi\">1<\/span><span class=\"p\">)<\/span>\n<span class=\"p\">{<\/span>\n        <span class=\"k\">exit<\/span> <span class=\"s2\">\"Enabled\"<\/span><span class=\"p\">;<\/span>\n<span class=\"p\">}<\/span>\n<\/pre>\n<\/div>\n<\/div>\n<p>On some servers where <em>display_errors<\/em> is disabled, and you do not have the ability to change this in the php.ini, you can often enable it with:<\/p>\n<div class=\"highlight-ci\">\n<div class=\"highlight\">\n<pre><span class=\"nb\">ini_set<\/span><span class=\"p\">(<\/span><span class=\"s1\">'display_errors'<\/span><span class=\"p\">,<\/span> <span class=\"mi\">1<\/span><span class=\"p\">);<\/span>\n<\/pre>\n<\/div>\n<\/div>\n<div class=\"admonition note\">\n<p class=\"first admonition-title\">Note<\/p>\n<p class=\"last\">Setting the <a class=\"reference external\" href=\"http:\/\/php.net\/manual\/en\/errorfunc.configuration.php#ini.display-errors\">display_errors<\/a> setting with <tt class=\"docutils literal\"><span class=\"pre\">ini_set()<\/span><\/tt> 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>\n<\/div>\n<\/div>\n<div id=\"short-open-tags\" class=\"section\">\n<h2><a class=\"toc-backref\" href=\"http:\/\/www.codeigniter.com\/user_guide\/general\/styleguide.html#id25\">Short Open Tags<\/a><a class=\"headerlink\" title=\"Permalink to this headline\" href=\"http:\/\/www.codeigniter.com\/user_guide\/general\/styleguide.html#short-open-tags\">\u00b6<\/a><\/h2>\n<p>Always use full PHP opening tags, in case a server does not have <em>short_open_tag<\/em> enabled.<br \/>\n<strong>INCORRECT<\/strong>:<\/p>\n<div class=\"highlight-ci\">\n<div class=\"highlight\">\n<pre><span class=\"o\">&lt;?<\/span> <span class=\"k\">echo<\/span> <span class=\"nv\">$foo<\/span><span class=\"p\">;<\/span> <span class=\"cp\">?&gt;<\/span>\n<span class=\"cp\">&lt;?<\/span><span class=\"o\">=<\/span><span class=\"nv\">$foo<\/span><span class=\"cp\">?&gt;<\/span>\n<\/pre>\n<\/div>\n<\/div>\n<p><strong>CORRECT<\/strong>:<\/p>\n<div class=\"highlight-ci\">\n<div class=\"highlight\">\n<pre><span class=\"o\">&lt;?<\/span><span class=\"nx\">php<\/span> <span class=\"k\">echo<\/span> <span class=\"nv\">$foo<\/span><span class=\"p\">;<\/span> <span class=\"cp\">?&gt;<\/span>\n<\/pre>\n<\/div>\n<\/div>\n<div class=\"admonition note\">\n<p class=\"first admonition-title\">Note<\/p>\n<p class=\"last\">PHP 5.4 will always have the <strong>&lt;?=<\/strong> tag available.<\/p>\n<\/div>\n<\/div>\n<div id=\"one-statement-per-line\" class=\"section\">\n<h2><a class=\"toc-backref\" href=\"http:\/\/www.codeigniter.com\/user_guide\/general\/styleguide.html#id26\">One Statement Per Line<\/a><a class=\"headerlink\" title=\"Permalink to this headline\" href=\"http:\/\/www.codeigniter.com\/user_guide\/general\/styleguide.html#one-statement-per-line\">\u00b6<\/a><\/h2>\n<p>Never combine statements on one line.<br \/>\n<strong>INCORRECT<\/strong>:<\/p>\n<div class=\"highlight-ci\">\n<div class=\"highlight\">\n<pre><span class=\"nv\">$foo<\/span> <span class=\"o\">=<\/span> <span class=\"s1\">'this'<\/span><span class=\"p\">;<\/span> <span class=\"nv\">$bar<\/span> <span class=\"o\">=<\/span> <span class=\"s1\">'that'<\/span><span class=\"p\">;<\/span> <span class=\"nv\">$bat<\/span> <span class=\"o\">=<\/span> <span class=\"nb\">str_replace<\/span><span class=\"p\">(<\/span><span class=\"nv\">$foo<\/span><span class=\"p\">,<\/span> <span class=\"nv\">$bar<\/span><span class=\"p\">,<\/span> <span class=\"nv\">$bag<\/span><span class=\"p\">);<\/span>\n<\/pre>\n<\/div>\n<\/div>\n<p><strong>CORRECT<\/strong>:<\/p>\n<div class=\"highlight-ci\">\n<div class=\"highlight\">\n<pre><span class=\"nv\">$foo<\/span> <span class=\"o\">=<\/span> <span class=\"s1\">'this'<\/span><span class=\"p\">;<\/span>\n<span class=\"nv\">$bar<\/span> <span class=\"o\">=<\/span> <span class=\"s1\">'that'<\/span><span class=\"p\">;<\/span>\n<span class=\"nv\">$bat<\/span> <span class=\"o\">=<\/span> <span class=\"nb\">str_replace<\/span><span class=\"p\">(<\/span><span class=\"nv\">$foo<\/span><span class=\"p\">,<\/span> <span class=\"nv\">$bar<\/span><span class=\"p\">,<\/span> <span class=\"nv\">$bag<\/span><span class=\"p\">);<\/span>\n<\/pre>\n<\/div>\n<\/div>\n<\/div>\n<div id=\"strings\" class=\"section\">\n<h2><a class=\"toc-backref\" href=\"http:\/\/www.codeigniter.com\/user_guide\/general\/styleguide.html#id27\">Strings<\/a><a class=\"headerlink\" title=\"Permalink to this headline\" href=\"http:\/\/www.codeigniter.com\/user_guide\/general\/styleguide.html#strings\">\u00b6<\/a><\/h2>\n<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.<br \/>\n<strong>INCORRECT<\/strong>:<\/p>\n<div class=\"highlight-ci\">\n<div class=\"highlight\">\n<pre><span class=\"s2\">\"My String\"<\/span>                                     <span class=\"c1\">\/\/ no variable parsing, so no use for double quotes<\/span>\n<span class=\"s2\">\"My string <\/span><span class=\"si\">$foo<\/span><span class=\"s2\">\"<\/span>                                <span class=\"c1\">\/\/ needs braces<\/span>\n<span class=\"s1\">'SELECT foo FROM bar WHERE baz = \\'bag\\''<\/span>       <span class=\"c1\">\/\/ ugly<\/span>\n<\/pre>\n<\/div>\n<\/div>\n<p><strong>CORRECT<\/strong>:<\/p>\n<div class=\"highlight-ci\">\n<div class=\"highlight\">\n<pre><span class=\"s1\">'My String'<\/span>\n<span class=\"s2\">\"My string <\/span><span class=\"si\">{<\/span><span class=\"nv\">$foo<\/span><span class=\"si\">}<\/span><span class=\"s2\">\"<\/span>\n<span class=\"s2\">\"SELECT foo FROM bar WHERE baz = 'bag'\"<\/span>\n<\/pre>\n<\/div>\n<\/div>\n<\/div>\n<div id=\"sql-queries\" class=\"section\">\n<h2><a class=\"toc-backref\" href=\"http:\/\/www.codeigniter.com\/user_guide\/general\/styleguide.html#id28\">SQL Queries<\/a><a class=\"headerlink\" title=\"Permalink to this headline\" href=\"http:\/\/www.codeigniter.com\/user_guide\/general\/styleguide.html#sql-queries\">\u00b6<\/a><\/h2>\n<p>SQL keywords are always capitalized: SELECT, INSERT, UPDATE, WHERE, AS, JOIN, ON, IN, etc.<br \/>\nBreak up long queries into multiple lines for legibility, preferably breaking for each clause.<br \/>\n<strong>INCORRECT<\/strong>:<\/p>\n<div class=\"highlight-ci\">\n<div class=\"highlight\">\n<pre><span class=\"c1\">\/\/ keywords are lowercase and query is too long for<\/span>\n<span class=\"c1\">\/\/ a single line (... indicates continuation of line)<\/span>\n<span class=\"nv\">$query<\/span> <span class=\"o\">=<\/span> <span class=\"nv\">$this<\/span><span class=\"o\">-&gt;<\/span><span class=\"na\">db<\/span><span class=\"o\">-&gt;<\/span><span class=\"na\">query<\/span><span class=\"p\">(<\/span><span class=\"s2\">\"select foo, bar, baz, foofoo, foobar as raboof, foobaz from exp_pre_email_addresses<\/span>\n<span class=\"s2\">...where foo != 'oof' and baz != 'zab' order by foobaz limit 5, 100\"<\/span><span class=\"p\">);<\/span>\n<\/pre>\n<\/div>\n<\/div>\n<p><strong>CORRECT<\/strong>:<\/p>\n<div class=\"highlight-ci\">\n<div class=\"highlight\">\n<pre><span class=\"nv\">$query<\/span> <span class=\"o\">=<\/span> <span class=\"nv\">$this<\/span><span class=\"o\">-&gt;<\/span><span class=\"na\">db<\/span><span class=\"o\">-&gt;<\/span><span class=\"na\">query<\/span><span class=\"p\">(<\/span><span class=\"s2\">\"SELECT foo, bar, baz, foofoo, foobar AS raboof, foobaz<\/span>\n<span class=\"s2\">                                FROM exp_pre_email_addresses<\/span>\n<span class=\"s2\">                                WHERE foo != 'oof'<\/span>\n<span class=\"s2\">                                AND baz != 'zab'<\/span>\n<span class=\"s2\">                                ORDER BY foobaz<\/span>\n<span class=\"s2\">                                LIMIT 5, 100\"<\/span><span class=\"p\">);<\/span>\n<\/pre>\n<\/div>\n<\/div>\n<\/div>\n<div id=\"default-function-arguments\" class=\"section\">\n<h2><a class=\"toc-backref\" href=\"http:\/\/www.codeigniter.com\/user_guide\/general\/styleguide.html#id29\">Default Function Arguments<\/a><a class=\"headerlink\" title=\"Permalink to this headline\" href=\"http:\/\/www.codeigniter.com\/user_guide\/general\/styleguide.html#default-function-arguments\">\u00b6<\/a><\/h2>\n<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>\n<div class=\"highlight-ci\">\n<div class=\"highlight\">\n<pre class=\"\"><span class=\"k\">function<\/span> <span class=\"nf\">foo<\/span><span class=\"p\">(<\/span><span class=\"nv\">$bar<\/span> <span class=\"o\">=<\/span> <span class=\"s1\">''<\/span><span class=\"p\">,<\/span> <span class=\"nv\">$baz<\/span> <span class=\"o\">=<\/span> <span class=\"k\">FALSE<\/span><span class=\"p\">)<\/span>\n<\/pre>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u6765\u81eahttp:\/\/www.codeigniter.com\/user_guide\/general\/styleguide.html\uff0c\u611f\u89c9\u8fd8\u633a\u6709\u7528\u7684\u3002 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. Table of Contents PHP Style Guide File Format TextMate BBEdit PHP Closing Tag File Naming Class and Method Naming Variable Names [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[31,85],"class_list":["post-1487","post","type-post","status-publish","format-standard","hentry","category-technical","tag-codeigniter","tag-php"],"_links":{"self":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts\/1487","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1487"}],"version-history":[{"count":0,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts\/1487\/revisions"}],"wp:attachment":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1487"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1487"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1487"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}