styleguide.html 79 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128
  1. <!DOCTYPE html>
  2. <!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
  3. <!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
  4. <head>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>PHP Style Guide &mdash; CodeIgniter 3.1.11 documentation</title>
  8. <link rel="shortcut icon" href="../_static/ci-icon.ico"/>
  9. <link href='https://fonts.googleapis.com/css?family=Lato:400,700,400italic,700italic|Roboto+Slab:400,700|Inconsolata:400,700&subset=latin,cyrillic' rel='stylesheet' type='text/css'>
  10. <link rel="stylesheet" href="../_static/css/citheme.css" type="text/css" />
  11. <link rel="index" title="Index"
  12. href="../genindex.html"/>
  13. <link rel="search" title="Search" href="../search.html"/>
  14. <link rel="top" title="CodeIgniter 3.1.11 documentation" href="../index.html"/>
  15. <link rel="up" title="General Topics" href="index.html"/>
  16. <link rel="next" title="Libraries" href="../libraries/index.html"/>
  17. <link rel="prev" title="Security" href="security.html"/>
  18. <script src="https://cdnjs.cloudflare.com/ajax/libs/modernizr/2.6.2/modernizr.min.js"></script>
  19. </head>
  20. <body class="wy-body-for-nav" role="document">
  21. <div id="nav">
  22. <div id="nav_inner">
  23. <div id="pulldown-menu" class="ciNav">
  24. <ul>
  25. <li class="toctree-l1"><a class="reference internal" href="welcome.html">Welcome to CodeIgniter</a></li>
  26. </ul>
  27. <ul>
  28. <li class="toctree-l1"><a class="reference internal" href="../installation/index.html">Installation Instructions</a><ul>
  29. <li class="toctree-l2"><a class="reference internal" href="../installation/downloads.html">Downloading CodeIgniter</a></li>
  30. <li class="toctree-l2"><a class="reference internal" href="../installation/index.html">Installation Instructions</a></li>
  31. <li class="toctree-l2"><a class="reference internal" href="../installation/upgrading.html">Upgrading From a Previous Version</a></li>
  32. <li class="toctree-l2"><a class="reference internal" href="../installation/troubleshooting.html">Troubleshooting</a></li>
  33. </ul>
  34. </li>
  35. </ul>
  36. <ul>
  37. <li class="toctree-l1"><a class="reference internal" href="../overview/index.html">CodeIgniter Overview</a><ul>
  38. <li class="toctree-l2"><a class="reference internal" href="../overview/getting_started.html">Getting Started</a></li>
  39. <li class="toctree-l2"><a class="reference internal" href="../overview/at_a_glance.html">CodeIgniter at a Glance</a></li>
  40. <li class="toctree-l2"><a class="reference internal" href="../overview/features.html">Supported Features</a></li>
  41. <li class="toctree-l2"><a class="reference internal" href="../overview/appflow.html">Application Flow Chart</a></li>
  42. <li class="toctree-l2"><a class="reference internal" href="../overview/mvc.html">Model-View-Controller</a></li>
  43. <li class="toctree-l2"><a class="reference internal" href="../overview/goals.html">Architectural Goals</a></li>
  44. </ul>
  45. </li>
  46. </ul>
  47. <ul>
  48. <li class="toctree-l1"><a class="reference internal" href="../tutorial/index.html">Tutorial</a><ul>
  49. <li class="toctree-l2"><a class="reference internal" href="../tutorial/static_pages.html">Static pages</a></li>
  50. <li class="toctree-l2"><a class="reference internal" href="../tutorial/news_section.html">News section</a></li>
  51. <li class="toctree-l2"><a class="reference internal" href="../tutorial/create_news_items.html">Create news items</a></li>
  52. <li class="toctree-l2"><a class="reference internal" href="../tutorial/conclusion.html">Conclusion</a></li>
  53. </ul>
  54. </li>
  55. </ul>
  56. <ul>
  57. <li class="toctree-l1"><a class="reference internal" href="../contributing/index.html">Contributing to CodeIgniter</a><ul>
  58. <li class="toctree-l2"><a class="reference internal" href="../documentation/index.html">Writing CodeIgniter Documentation</a></li>
  59. <li class="toctree-l2"><a class="reference internal" href="../DCO.html">Developer’s Certificate of Origin 1.1</a></li>
  60. </ul>
  61. </li>
  62. </ul>
  63. <ul class="current">
  64. <li class="toctree-l1 current"><a class="reference internal" href="index.html">General Topics</a><ul class="current">
  65. <li class="toctree-l2"><a class="reference internal" href="urls.html">CodeIgniter URLs</a></li>
  66. <li class="toctree-l2"><a class="reference internal" href="controllers.html">Controllers</a></li>
  67. <li class="toctree-l2"><a class="reference internal" href="reserved_names.html">Reserved Names</a></li>
  68. <li class="toctree-l2"><a class="reference internal" href="views.html">Views</a></li>
  69. <li class="toctree-l2"><a class="reference internal" href="models.html">Models</a></li>
  70. <li class="toctree-l2"><a class="reference internal" href="helpers.html">Helpers</a></li>
  71. <li class="toctree-l2"><a class="reference internal" href="libraries.html">Using CodeIgniter Libraries</a></li>
  72. <li class="toctree-l2"><a class="reference internal" href="creating_libraries.html">Creating Libraries</a></li>
  73. <li class="toctree-l2"><a class="reference internal" href="drivers.html">Using CodeIgniter Drivers</a></li>
  74. <li class="toctree-l2"><a class="reference internal" href="creating_drivers.html">Creating Drivers</a></li>
  75. <li class="toctree-l2"><a class="reference internal" href="core_classes.html">Creating Core System Classes</a></li>
  76. <li class="toctree-l2"><a class="reference internal" href="ancillary_classes.html">Creating Ancillary Classes</a></li>
  77. <li class="toctree-l2"><a class="reference internal" href="hooks.html">Hooks - Extending the Framework Core</a></li>
  78. <li class="toctree-l2"><a class="reference internal" href="autoloader.html">Auto-loading Resources</a></li>
  79. <li class="toctree-l2"><a class="reference internal" href="common_functions.html">Common Functions</a></li>
  80. <li class="toctree-l2"><a class="reference internal" href="compatibility_functions.html">Compatibility Functions</a></li>
  81. <li class="toctree-l2"><a class="reference internal" href="routing.html">URI Routing</a></li>
  82. <li class="toctree-l2"><a class="reference internal" href="errors.html">Error Handling</a></li>
  83. <li class="toctree-l2"><a class="reference internal" href="caching.html">Caching</a></li>
  84. <li class="toctree-l2"><a class="reference internal" href="profiling.html">Profiling Your Application</a></li>
  85. <li class="toctree-l2"><a class="reference internal" href="cli.html">Running via the CLI</a></li>
  86. <li class="toctree-l2"><a class="reference internal" href="managing_apps.html">Managing your Applications</a></li>
  87. <li class="toctree-l2"><a class="reference internal" href="environments.html">Handling Multiple Environments</a></li>
  88. <li class="toctree-l2"><a class="reference internal" href="alternative_php.html">Alternate PHP Syntax for View Files</a></li>
  89. <li class="toctree-l2"><a class="reference internal" href="security.html">Security</a></li>
  90. <li class="toctree-l2 current"><a class="current reference internal" href="#">PHP Style Guide</a></li>
  91. </ul>
  92. </li>
  93. </ul>
  94. <ul>
  95. <li class="toctree-l1"><a class="reference internal" href="../libraries/index.html">Libraries</a><ul>
  96. <li class="toctree-l2"><a class="reference internal" href="../libraries/benchmark.html">Benchmarking Class</a></li>
  97. <li class="toctree-l2"><a class="reference internal" href="../libraries/caching.html">Caching Driver</a></li>
  98. <li class="toctree-l2"><a class="reference internal" href="../libraries/calendar.html">Calendaring Class</a></li>
  99. <li class="toctree-l2"><a class="reference internal" href="../libraries/cart.html">Shopping Cart Class</a></li>
  100. <li class="toctree-l2"><a class="reference internal" href="../libraries/config.html">Config Class</a></li>
  101. <li class="toctree-l2"><a class="reference internal" href="../libraries/email.html">Email Class</a></li>
  102. <li class="toctree-l2"><a class="reference internal" href="../libraries/encrypt.html">Encrypt Class</a></li>
  103. <li class="toctree-l2"><a class="reference internal" href="../libraries/encryption.html">Encryption Library</a></li>
  104. <li class="toctree-l2"><a class="reference internal" href="../libraries/file_uploading.html">File Uploading Class</a></li>
  105. <li class="toctree-l2"><a class="reference internal" href="../libraries/form_validation.html">Form Validation</a></li>
  106. <li class="toctree-l2"><a class="reference internal" href="../libraries/ftp.html">FTP Class</a></li>
  107. <li class="toctree-l2"><a class="reference internal" href="../libraries/image_lib.html">Image Manipulation Class</a></li>
  108. <li class="toctree-l2"><a class="reference internal" href="../libraries/input.html">Input Class</a></li>
  109. <li class="toctree-l2"><a class="reference internal" href="../libraries/javascript.html">Javascript Class</a></li>
  110. <li class="toctree-l2"><a class="reference internal" href="../libraries/language.html">Language Class</a></li>
  111. <li class="toctree-l2"><a class="reference internal" href="../libraries/loader.html">Loader Class</a></li>
  112. <li class="toctree-l2"><a class="reference internal" href="../libraries/migration.html">Migrations Class</a></li>
  113. <li class="toctree-l2"><a class="reference internal" href="../libraries/output.html">Output Class</a></li>
  114. <li class="toctree-l2"><a class="reference internal" href="../libraries/pagination.html">Pagination Class</a></li>
  115. <li class="toctree-l2"><a class="reference internal" href="../libraries/parser.html">Template Parser Class</a></li>
  116. <li class="toctree-l2"><a class="reference internal" href="../libraries/security.html">Security Class</a></li>
  117. <li class="toctree-l2"><a class="reference internal" href="../libraries/sessions.html">Session Library</a></li>
  118. <li class="toctree-l2"><a class="reference internal" href="../libraries/table.html">HTML Table Class</a></li>
  119. <li class="toctree-l2"><a class="reference internal" href="../libraries/trackback.html">Trackback Class</a></li>
  120. <li class="toctree-l2"><a class="reference internal" href="../libraries/typography.html">Typography Class</a></li>
  121. <li class="toctree-l2"><a class="reference internal" href="../libraries/unit_testing.html">Unit Testing Class</a></li>
  122. <li class="toctree-l2"><a class="reference internal" href="../libraries/uri.html">URI Class</a></li>
  123. <li class="toctree-l2"><a class="reference internal" href="../libraries/user_agent.html">User Agent Class</a></li>
  124. <li class="toctree-l2"><a class="reference internal" href="../libraries/xmlrpc.html">XML-RPC and XML-RPC Server Classes</a></li>
  125. <li class="toctree-l2"><a class="reference internal" href="../libraries/zip.html">Zip Encoding Class</a></li>
  126. </ul>
  127. </li>
  128. </ul>
  129. <ul>
  130. <li class="toctree-l1"><a class="reference internal" href="../database/index.html">Database Reference</a><ul>
  131. <li class="toctree-l2"><a class="reference internal" href="../database/examples.html">Quick Start: Usage Examples</a></li>
  132. <li class="toctree-l2"><a class="reference internal" href="../database/configuration.html">Database Configuration</a></li>
  133. <li class="toctree-l2"><a class="reference internal" href="../database/connecting.html">Connecting to a Database</a></li>
  134. <li class="toctree-l2"><a class="reference internal" href="../database/queries.html">Running Queries</a></li>
  135. <li class="toctree-l2"><a class="reference internal" href="../database/results.html">Generating Query Results</a></li>
  136. <li class="toctree-l2"><a class="reference internal" href="../database/helpers.html">Query Helper Functions</a></li>
  137. <li class="toctree-l2"><a class="reference internal" href="../database/query_builder.html">Query Builder Class</a></li>
  138. <li class="toctree-l2"><a class="reference internal" href="../database/transactions.html">Transactions</a></li>
  139. <li class="toctree-l2"><a class="reference internal" href="../database/metadata.html">Getting MetaData</a></li>
  140. <li class="toctree-l2"><a class="reference internal" href="../database/call_function.html">Custom Function Calls</a></li>
  141. <li class="toctree-l2"><a class="reference internal" href="../database/caching.html">Query Caching</a></li>
  142. <li class="toctree-l2"><a class="reference internal" href="../database/forge.html">Database Manipulation with Database Forge</a></li>
  143. <li class="toctree-l2"><a class="reference internal" href="../database/utilities.html">Database Utilities Class</a></li>
  144. <li class="toctree-l2"><a class="reference internal" href="../database/db_driver_reference.html">Database Driver Reference</a></li>
  145. </ul>
  146. </li>
  147. </ul>
  148. <ul>
  149. <li class="toctree-l1"><a class="reference internal" href="../helpers/index.html">Helpers</a><ul>
  150. <li class="toctree-l2"><a class="reference internal" href="../helpers/array_helper.html">Array Helper</a></li>
  151. <li class="toctree-l2"><a class="reference internal" href="../helpers/captcha_helper.html">CAPTCHA Helper</a></li>
  152. <li class="toctree-l2"><a class="reference internal" href="../helpers/cookie_helper.html">Cookie Helper</a></li>
  153. <li class="toctree-l2"><a class="reference internal" href="../helpers/date_helper.html">Date Helper</a></li>
  154. <li class="toctree-l2"><a class="reference internal" href="../helpers/directory_helper.html">Directory Helper</a></li>
  155. <li class="toctree-l2"><a class="reference internal" href="../helpers/download_helper.html">Download Helper</a></li>
  156. <li class="toctree-l2"><a class="reference internal" href="../helpers/email_helper.html">Email Helper</a></li>
  157. <li class="toctree-l2"><a class="reference internal" href="../helpers/file_helper.html">File Helper</a></li>
  158. <li class="toctree-l2"><a class="reference internal" href="../helpers/form_helper.html">Form Helper</a></li>
  159. <li class="toctree-l2"><a class="reference internal" href="../helpers/html_helper.html">HTML Helper</a></li>
  160. <li class="toctree-l2"><a class="reference internal" href="../helpers/inflector_helper.html">Inflector Helper</a></li>
  161. <li class="toctree-l2"><a class="reference internal" href="../helpers/language_helper.html">Language Helper</a></li>
  162. <li class="toctree-l2"><a class="reference internal" href="../helpers/number_helper.html">Number Helper</a></li>
  163. <li class="toctree-l2"><a class="reference internal" href="../helpers/path_helper.html">Path Helper</a></li>
  164. <li class="toctree-l2"><a class="reference internal" href="../helpers/security_helper.html">Security Helper</a></li>
  165. <li class="toctree-l2"><a class="reference internal" href="../helpers/smiley_helper.html">Smiley Helper</a></li>
  166. <li class="toctree-l2"><a class="reference internal" href="../helpers/string_helper.html">String Helper</a></li>
  167. <li class="toctree-l2"><a class="reference internal" href="../helpers/text_helper.html">Text Helper</a></li>
  168. <li class="toctree-l2"><a class="reference internal" href="../helpers/typography_helper.html">Typography Helper</a></li>
  169. <li class="toctree-l2"><a class="reference internal" href="../helpers/url_helper.html">URL Helper</a></li>
  170. <li class="toctree-l2"><a class="reference internal" href="../helpers/xml_helper.html">XML Helper</a></li>
  171. </ul>
  172. </li>
  173. </ul>
  174. </div>
  175. </div>
  176. </div>
  177. <div id="nav2">
  178. <a href="#" id="openToc">
  179. <img src="data:image/jpeg;base64,/9j/4AAQSkZJRgABAgAAZABkAAD/7AARRHVja3kAAQAEAAAARgAA/+4ADkFkb2JlAGTAAAAAAf/bAIQABAMDAwMDBAMDBAYEAwQGBwUEBAUHCAYGBwYGCAoICQkJCQgKCgwMDAwMCgwMDQ0MDBERERERFBQUFBQUFBQUFAEEBQUIBwgPCgoPFA4ODhQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQU/8AAEQgAKwCaAwERAAIRAQMRAf/EAHsAAQAABwEBAAAAAAAAAAAAAAABAwQFBgcIAgkBAQAAAAAAAAAAAAAAAAAAAAAQAAEDAwICBwYEAgsAAAAAAAIBAwQAEQUSBiEHkROTVNQWGDFBUVIUCHEiMtOUFWGBobHRQlMkZIRVEQEAAAAAAAAAAAAAAAAAAAAA/9oADAMBAAIRAxEAPwDSC+ygkOOaUoKigUCgUCgUCgUCgUCgUCgUCgkuGguIP9FBMFb0Hqg7We+3jlmIqqYFf4ub+/QYlnOR/LqIBKGFUbf8qWv971BytQXXE7Y3Lnm3HsFhp2TaZJAdchRXpIgSpdEJWxJEW3xoKV7F5OMy7JkQn2o7D6w33XGjEAkoiqrJEqIiOIiKuhePCgqp22dyYyS3CyWHnQ5joG61HkRnmnTbaFSMhExRVQRRVJU9iUHjE7ez+fJ0MFipmUNhBV8YUd2SoIV9KkjQla9ltegttBdPLW4/qocL+UTfrMiHW4+P9M71shuyrqaHTcxsl7jegpsji8nh5ZwMvDfgTm0RTjSmjYdFCS6KoOIipdFunCgmNYTMv457MMY6U7iI6oMieDDhRm1VbIhuoOkbqtuK0Hpzb+eZcYZexUxt6UyUqK2cd0SdjtgrhOgijcgERUlJOCIl6CpgbP3blRI8XgMjNARAyKNDfeRBdFDBVUAXgQrqH4pxoJTu2NysY97LP4ac1io5q1InHFeGO24LnVKJuKOkSQ/yKir+rh7aCLG1dzypZQI2FnvTgccYOM3FeN0XWERXAUEFVQgQkUktdLpegm+Td3/Xli/L+S/mYNJIOF9G/wBeLKrZHFb0akG6W1WtQWSg3Dyg5e7V3fipE3O4/wCrktyzYA+ufas2LbZIlmnAT2kvuoN1wft95augilglX/tzP3qCu9O3LL/wV/i5v79BvmTADq14UGu91467Z6U9y0HzH/ncj/U/sT/CgynZG7I2NezpZGUjIycJkYkZSG+uQ81pbBNKLxJfjwoMqZ3/ALYHl35AJ7/cuwHcu5k7r1Q5pHetBjquqVVJWGxj9Zrtcl/Ggy3dHMvauR3HFZj5nHNxSyW5JISYDMoIwx8tFIGHZhPNaykGapr6rUAiicEoMG21lMRj8buPAz8xhJrr7uOeiPTCyAwXUaGR1mgozbTusOsFLEiJ7fbQa/h7gcjy2H3V6xppwDNtUSxCJIqp7valBuWVzJ22xuCROXNNZiJkMtms0DbjUkAZjzoDrTMd9dDRI44ZC2YsrYdKWP2WDT2S3N9dNdlRYrGMYc06IURXSYb0igrpWS485xVNS6nF4rwslkoMwnbpgZLB7bmt5uMweAhDEl4B5uSLzzqTnnyVpW2jaJHRMSIjdDiiotvy3DOE5rYTEbkl5yFn28k7JyG4c7AU2HtLH1uKfaiMPI40CdYbpNtmLdwTSn5rewLNld+7TLdeal4WarWBkbVKBjgdElMJJwAAY5fl4kB3b1fp4XvagsGS3FjJfLzDNtS8aeXx7LzT7TyzByQE5PccRGRC0ZRUDRV6y62vbjagzLmJzS2vuPK43JY6aP1TW6Jz+RIWyFtyC06y3EkiiinAo7YCqfq1AqqnGgsOH3lhZO8d1pmcpB8j5XIm9OYlBJSQ/FSS4427DKO0RC8AlcEMhFdViRR1WDWR5t3WXVuL1d106kG9vdeye2g60+1FDyW0shIcXVpyroXt8I8dfd+NB1vioAdWnD3UF1+gD4UFc6CEKpagxXN43rwJLUHz7yX2c8zokt9uHlsPIhA4aRnnHJTLptIS6CNsY7iASpxUUMkReGpfbQW0vtN5pitvrsN28rwtBD0nc0+/Yft5XhaB6TuaXfsP28rwtA9J3NPv2H7eV4Wgek7mn37D9vK8LQPSdzT79h+3leFoHpO5pd+w/byvC0D0nc0u/Yft5XhaB6TuaXfsP28rwtA9J3NLv2H7eV4Wgek7ml37D9vK8LQPSdzS79h+3leFoHpO5p9+w/byvC0E9r7Reazy2HIYVPxkS/CUHVn26cosxyv2g7h89LYmZSXOenvLEQ1YaQ222RATcQCP8rSGqqA8S02W2pQ6FhMoAIlqCtsnwoCpdKClejI4i3Sgtb+GBxVuNBSFt1pV/RQefLjPyUDy4z8lA8uM/JQPLjPyUDy4z8lA8uM/JQPLjPyUDy4z8lA8uM/JQPLjPyUDy4z8lA8utJ/koJ7WCbBU/LQXOPAFq1koK8B0pag90CggtBBf6qB0UDooHRQOigdFA6KB0UDooHRQOigdFA6KB0UDooI0EaBQf//Z" title="Toggle Table of Contents" alt="Toggle Table of Contents" />
  180. </a>
  181. </div>
  182. <div class="wy-grid-for-nav">
  183. <nav data-toggle="wy-nav-shift" class="wy-nav-side">
  184. <div class="wy-side-nav-search">
  185. <a href="../index.html" class="fa fa-home"> CodeIgniter</a>
  186. <div role="search">
  187. <form id="rtd-search-form" class="wy-form" action="../search.html" method="get">
  188. <input type="text" name="q" placeholder="Search docs" />
  189. <input type="hidden" name="check_keywords" value="yes" />
  190. <input type="hidden" name="area" value="default" />
  191. </form>
  192. </div>
  193. </div>
  194. <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
  195. <ul>
  196. <li class="toctree-l1"><a class="reference internal" href="welcome.html">Welcome to CodeIgniter</a></li>
  197. </ul>
  198. <ul>
  199. <li class="toctree-l1"><a class="reference internal" href="../installation/index.html">Installation Instructions</a><ul>
  200. <li class="toctree-l2"><a class="reference internal" href="../installation/downloads.html">Downloading CodeIgniter</a></li>
  201. <li class="toctree-l2"><a class="reference internal" href="../installation/index.html">Installation Instructions</a></li>
  202. <li class="toctree-l2"><a class="reference internal" href="../installation/upgrading.html">Upgrading From a Previous Version</a></li>
  203. <li class="toctree-l2"><a class="reference internal" href="../installation/troubleshooting.html">Troubleshooting</a></li>
  204. </ul>
  205. </li>
  206. </ul>
  207. <ul>
  208. <li class="toctree-l1"><a class="reference internal" href="../overview/index.html">CodeIgniter Overview</a><ul>
  209. <li class="toctree-l2"><a class="reference internal" href="../overview/getting_started.html">Getting Started</a></li>
  210. <li class="toctree-l2"><a class="reference internal" href="../overview/at_a_glance.html">CodeIgniter at a Glance</a></li>
  211. <li class="toctree-l2"><a class="reference internal" href="../overview/features.html">Supported Features</a></li>
  212. <li class="toctree-l2"><a class="reference internal" href="../overview/appflow.html">Application Flow Chart</a></li>
  213. <li class="toctree-l2"><a class="reference internal" href="../overview/mvc.html">Model-View-Controller</a></li>
  214. <li class="toctree-l2"><a class="reference internal" href="../overview/goals.html">Architectural Goals</a></li>
  215. </ul>
  216. </li>
  217. </ul>
  218. <ul>
  219. <li class="toctree-l1"><a class="reference internal" href="../tutorial/index.html">Tutorial</a><ul>
  220. <li class="toctree-l2"><a class="reference internal" href="../tutorial/static_pages.html">Static pages</a></li>
  221. <li class="toctree-l2"><a class="reference internal" href="../tutorial/news_section.html">News section</a></li>
  222. <li class="toctree-l2"><a class="reference internal" href="../tutorial/create_news_items.html">Create news items</a></li>
  223. <li class="toctree-l2"><a class="reference internal" href="../tutorial/conclusion.html">Conclusion</a></li>
  224. </ul>
  225. </li>
  226. </ul>
  227. <ul>
  228. <li class="toctree-l1"><a class="reference internal" href="../contributing/index.html">Contributing to CodeIgniter</a><ul>
  229. <li class="toctree-l2"><a class="reference internal" href="../documentation/index.html">Writing CodeIgniter Documentation</a></li>
  230. <li class="toctree-l2"><a class="reference internal" href="../DCO.html">Developer’s Certificate of Origin 1.1</a></li>
  231. </ul>
  232. </li>
  233. </ul>
  234. <ul class="current">
  235. <li class="toctree-l1 current"><a class="reference internal" href="index.html">General Topics</a><ul class="current">
  236. <li class="toctree-l2"><a class="reference internal" href="urls.html">CodeIgniter URLs</a></li>
  237. <li class="toctree-l2"><a class="reference internal" href="controllers.html">Controllers</a></li>
  238. <li class="toctree-l2"><a class="reference internal" href="reserved_names.html">Reserved Names</a></li>
  239. <li class="toctree-l2"><a class="reference internal" href="views.html">Views</a></li>
  240. <li class="toctree-l2"><a class="reference internal" href="models.html">Models</a></li>
  241. <li class="toctree-l2"><a class="reference internal" href="helpers.html">Helpers</a></li>
  242. <li class="toctree-l2"><a class="reference internal" href="libraries.html">Using CodeIgniter Libraries</a></li>
  243. <li class="toctree-l2"><a class="reference internal" href="creating_libraries.html">Creating Libraries</a></li>
  244. <li class="toctree-l2"><a class="reference internal" href="drivers.html">Using CodeIgniter Drivers</a></li>
  245. <li class="toctree-l2"><a class="reference internal" href="creating_drivers.html">Creating Drivers</a></li>
  246. <li class="toctree-l2"><a class="reference internal" href="core_classes.html">Creating Core System Classes</a></li>
  247. <li class="toctree-l2"><a class="reference internal" href="ancillary_classes.html">Creating Ancillary Classes</a></li>
  248. <li class="toctree-l2"><a class="reference internal" href="hooks.html">Hooks - Extending the Framework Core</a></li>
  249. <li class="toctree-l2"><a class="reference internal" href="autoloader.html">Auto-loading Resources</a></li>
  250. <li class="toctree-l2"><a class="reference internal" href="common_functions.html">Common Functions</a></li>
  251. <li class="toctree-l2"><a class="reference internal" href="compatibility_functions.html">Compatibility Functions</a></li>
  252. <li class="toctree-l2"><a class="reference internal" href="routing.html">URI Routing</a></li>
  253. <li class="toctree-l2"><a class="reference internal" href="errors.html">Error Handling</a></li>
  254. <li class="toctree-l2"><a class="reference internal" href="caching.html">Caching</a></li>
  255. <li class="toctree-l2"><a class="reference internal" href="profiling.html">Profiling Your Application</a></li>
  256. <li class="toctree-l2"><a class="reference internal" href="cli.html">Running via the CLI</a></li>
  257. <li class="toctree-l2"><a class="reference internal" href="managing_apps.html">Managing your Applications</a></li>
  258. <li class="toctree-l2"><a class="reference internal" href="environments.html">Handling Multiple Environments</a></li>
  259. <li class="toctree-l2"><a class="reference internal" href="alternative_php.html">Alternate PHP Syntax for View Files</a></li>
  260. <li class="toctree-l2"><a class="reference internal" href="security.html">Security</a></li>
  261. <li class="toctree-l2 current"><a class="current reference internal" href="#">PHP Style Guide</a></li>
  262. </ul>
  263. </li>
  264. </ul>
  265. <ul>
  266. <li class="toctree-l1"><a class="reference internal" href="../libraries/index.html">Libraries</a><ul>
  267. <li class="toctree-l2"><a class="reference internal" href="../libraries/benchmark.html">Benchmarking Class</a></li>
  268. <li class="toctree-l2"><a class="reference internal" href="../libraries/caching.html">Caching Driver</a></li>
  269. <li class="toctree-l2"><a class="reference internal" href="../libraries/calendar.html">Calendaring Class</a></li>
  270. <li class="toctree-l2"><a class="reference internal" href="../libraries/cart.html">Shopping Cart Class</a></li>
  271. <li class="toctree-l2"><a class="reference internal" href="../libraries/config.html">Config Class</a></li>
  272. <li class="toctree-l2"><a class="reference internal" href="../libraries/email.html">Email Class</a></li>
  273. <li class="toctree-l2"><a class="reference internal" href="../libraries/encrypt.html">Encrypt Class</a></li>
  274. <li class="toctree-l2"><a class="reference internal" href="../libraries/encryption.html">Encryption Library</a></li>
  275. <li class="toctree-l2"><a class="reference internal" href="../libraries/file_uploading.html">File Uploading Class</a></li>
  276. <li class="toctree-l2"><a class="reference internal" href="../libraries/form_validation.html">Form Validation</a></li>
  277. <li class="toctree-l2"><a class="reference internal" href="../libraries/ftp.html">FTP Class</a></li>
  278. <li class="toctree-l2"><a class="reference internal" href="../libraries/image_lib.html">Image Manipulation Class</a></li>
  279. <li class="toctree-l2"><a class="reference internal" href="../libraries/input.html">Input Class</a></li>
  280. <li class="toctree-l2"><a class="reference internal" href="../libraries/javascript.html">Javascript Class</a></li>
  281. <li class="toctree-l2"><a class="reference internal" href="../libraries/language.html">Language Class</a></li>
  282. <li class="toctree-l2"><a class="reference internal" href="../libraries/loader.html">Loader Class</a></li>
  283. <li class="toctree-l2"><a class="reference internal" href="../libraries/migration.html">Migrations Class</a></li>
  284. <li class="toctree-l2"><a class="reference internal" href="../libraries/output.html">Output Class</a></li>
  285. <li class="toctree-l2"><a class="reference internal" href="../libraries/pagination.html">Pagination Class</a></li>
  286. <li class="toctree-l2"><a class="reference internal" href="../libraries/parser.html">Template Parser Class</a></li>
  287. <li class="toctree-l2"><a class="reference internal" href="../libraries/security.html">Security Class</a></li>
  288. <li class="toctree-l2"><a class="reference internal" href="../libraries/sessions.html">Session Library</a></li>
  289. <li class="toctree-l2"><a class="reference internal" href="../libraries/table.html">HTML Table Class</a></li>
  290. <li class="toctree-l2"><a class="reference internal" href="../libraries/trackback.html">Trackback Class</a></li>
  291. <li class="toctree-l2"><a class="reference internal" href="../libraries/typography.html">Typography Class</a></li>
  292. <li class="toctree-l2"><a class="reference internal" href="../libraries/unit_testing.html">Unit Testing Class</a></li>
  293. <li class="toctree-l2"><a class="reference internal" href="../libraries/uri.html">URI Class</a></li>
  294. <li class="toctree-l2"><a class="reference internal" href="../libraries/user_agent.html">User Agent Class</a></li>
  295. <li class="toctree-l2"><a class="reference internal" href="../libraries/xmlrpc.html">XML-RPC and XML-RPC Server Classes</a></li>
  296. <li class="toctree-l2"><a class="reference internal" href="../libraries/zip.html">Zip Encoding Class</a></li>
  297. </ul>
  298. </li>
  299. </ul>
  300. <ul>
  301. <li class="toctree-l1"><a class="reference internal" href="../database/index.html">Database Reference</a><ul>
  302. <li class="toctree-l2"><a class="reference internal" href="../database/examples.html">Quick Start: Usage Examples</a></li>
  303. <li class="toctree-l2"><a class="reference internal" href="../database/configuration.html">Database Configuration</a></li>
  304. <li class="toctree-l2"><a class="reference internal" href="../database/connecting.html">Connecting to a Database</a></li>
  305. <li class="toctree-l2"><a class="reference internal" href="../database/queries.html">Running Queries</a></li>
  306. <li class="toctree-l2"><a class="reference internal" href="../database/results.html">Generating Query Results</a></li>
  307. <li class="toctree-l2"><a class="reference internal" href="../database/helpers.html">Query Helper Functions</a></li>
  308. <li class="toctree-l2"><a class="reference internal" href="../database/query_builder.html">Query Builder Class</a></li>
  309. <li class="toctree-l2"><a class="reference internal" href="../database/transactions.html">Transactions</a></li>
  310. <li class="toctree-l2"><a class="reference internal" href="../database/metadata.html">Getting MetaData</a></li>
  311. <li class="toctree-l2"><a class="reference internal" href="../database/call_function.html">Custom Function Calls</a></li>
  312. <li class="toctree-l2"><a class="reference internal" href="../database/caching.html">Query Caching</a></li>
  313. <li class="toctree-l2"><a class="reference internal" href="../database/forge.html">Database Manipulation with Database Forge</a></li>
  314. <li class="toctree-l2"><a class="reference internal" href="../database/utilities.html">Database Utilities Class</a></li>
  315. <li class="toctree-l2"><a class="reference internal" href="../database/db_driver_reference.html">Database Driver Reference</a></li>
  316. </ul>
  317. </li>
  318. </ul>
  319. <ul>
  320. <li class="toctree-l1"><a class="reference internal" href="../helpers/index.html">Helpers</a><ul>
  321. <li class="toctree-l2"><a class="reference internal" href="../helpers/array_helper.html">Array Helper</a></li>
  322. <li class="toctree-l2"><a class="reference internal" href="../helpers/captcha_helper.html">CAPTCHA Helper</a></li>
  323. <li class="toctree-l2"><a class="reference internal" href="../helpers/cookie_helper.html">Cookie Helper</a></li>
  324. <li class="toctree-l2"><a class="reference internal" href="../helpers/date_helper.html">Date Helper</a></li>
  325. <li class="toctree-l2"><a class="reference internal" href="../helpers/directory_helper.html">Directory Helper</a></li>
  326. <li class="toctree-l2"><a class="reference internal" href="../helpers/download_helper.html">Download Helper</a></li>
  327. <li class="toctree-l2"><a class="reference internal" href="../helpers/email_helper.html">Email Helper</a></li>
  328. <li class="toctree-l2"><a class="reference internal" href="../helpers/file_helper.html">File Helper</a></li>
  329. <li class="toctree-l2"><a class="reference internal" href="../helpers/form_helper.html">Form Helper</a></li>
  330. <li class="toctree-l2"><a class="reference internal" href="../helpers/html_helper.html">HTML Helper</a></li>
  331. <li class="toctree-l2"><a class="reference internal" href="../helpers/inflector_helper.html">Inflector Helper</a></li>
  332. <li class="toctree-l2"><a class="reference internal" href="../helpers/language_helper.html">Language Helper</a></li>
  333. <li class="toctree-l2"><a class="reference internal" href="../helpers/number_helper.html">Number Helper</a></li>
  334. <li class="toctree-l2"><a class="reference internal" href="../helpers/path_helper.html">Path Helper</a></li>
  335. <li class="toctree-l2"><a class="reference internal" href="../helpers/security_helper.html">Security Helper</a></li>
  336. <li class="toctree-l2"><a class="reference internal" href="../helpers/smiley_helper.html">Smiley Helper</a></li>
  337. <li class="toctree-l2"><a class="reference internal" href="../helpers/string_helper.html">String Helper</a></li>
  338. <li class="toctree-l2"><a class="reference internal" href="../helpers/text_helper.html">Text Helper</a></li>
  339. <li class="toctree-l2"><a class="reference internal" href="../helpers/typography_helper.html">Typography Helper</a></li>
  340. <li class="toctree-l2"><a class="reference internal" href="../helpers/url_helper.html">URL Helper</a></li>
  341. <li class="toctree-l2"><a class="reference internal" href="../helpers/xml_helper.html">XML Helper</a></li>
  342. </ul>
  343. </li>
  344. </ul>
  345. </div>
  346. &nbsp;
  347. </nav>
  348. <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">
  349. <nav class="wy-nav-top" role="navigation" aria-label="top navigation">
  350. <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
  351. <a href="../index.html">CodeIgniter</a>
  352. </nav>
  353. <div class="wy-nav-content">
  354. <div class="rst-content">
  355. <div role="navigation" aria-label="breadcrumbs navigation">
  356. <ul class="wy-breadcrumbs">
  357. <li><a href="../index.html">Docs</a> &raquo;</li>
  358. <li><a href="index.html">General Topics</a> &raquo;</li>
  359. <li>PHP Style Guide</li>
  360. <li class="wy-breadcrumbs-aside">
  361. </li>
  362. <div style="float:right;margin-left:5px;" id="closeMe">
  363. <img title="Classic Layout" alt="classic layout" src="data:image/gif;base64,R0lGODlhFAAUAJEAAAAAADMzM////wAAACH5BAUUAAIALAAAAAAUABQAAAImlI+py+0PU5gRBRDM3DxbWoXis42X13USOLauUIqnlsaH/eY6UwAAOw==" />
  364. </div>
  365. </ul>
  366. <hr/>
  367. </div>
  368. <div role="main" class="document">
  369. <div class="section" id="php-style-guide">
  370. <h1><a class="toc-backref" href="#id1">PHP Style Guide</a><a class="headerlink" href="#php-style-guide" title="Permalink to this headline">¶</a></h1>
  371. <p>The following page describes the coding styles adhered to when
  372. contributing to the development of CodeIgniter. There is no requirement
  373. to use these styles in your own CodeIgniter application, though they
  374. are recommended.</p>
  375. <div class="contents topic" id="table-of-contents">
  376. <p class="topic-title first">Table of Contents</p>
  377. <ul class="simple">
  378. <li><a class="reference internal" href="#php-style-guide" id="id1">PHP Style Guide</a><ul>
  379. <li><a class="reference internal" href="#file-format" id="id2">File Format</a><ul>
  380. <li><a class="reference internal" href="#textmate" id="id3">TextMate</a></li>
  381. <li><a class="reference internal" href="#bbedit" id="id4">BBEdit</a></li>
  382. </ul>
  383. </li>
  384. <li><a class="reference internal" href="#php-closing-tag" id="id5">PHP Closing Tag</a></li>
  385. <li><a class="reference internal" href="#file-naming" id="id6">File Naming</a></li>
  386. <li><a class="reference internal" href="#class-and-method-naming" id="id7">Class and Method Naming</a></li>
  387. <li><a class="reference internal" href="#variable-names" id="id8">Variable Names</a></li>
  388. <li><a class="reference internal" href="#commenting" id="id9">Commenting</a></li>
  389. <li><a class="reference internal" href="#constants" id="id10">Constants</a></li>
  390. <li><a class="reference internal" href="#true-false-and-null" id="id11">TRUE, FALSE, and NULL</a></li>
  391. <li><a class="reference internal" href="#logical-operators" id="id12">Logical Operators</a></li>
  392. <li><a class="reference internal" href="#comparing-return-values-and-typecasting" id="id13">Comparing Return Values and Typecasting</a></li>
  393. <li><a class="reference internal" href="#debugging-code" id="id14">Debugging Code</a></li>
  394. <li><a class="reference internal" href="#whitespace-in-files" id="id15">Whitespace in Files</a></li>
  395. <li><a class="reference internal" href="#compatibility" id="id16">Compatibility</a></li>
  396. <li><a class="reference internal" href="#one-file-per-class" id="id17">One File per Class</a></li>
  397. <li><a class="reference internal" href="#whitespace" id="id18">Whitespace</a></li>
  398. <li><a class="reference internal" href="#line-breaks" id="id19">Line Breaks</a></li>
  399. <li><a class="reference internal" href="#code-indenting" id="id20">Code Indenting</a></li>
  400. <li><a class="reference internal" href="#bracket-and-parenthetic-spacing" id="id21">Bracket and Parenthetic Spacing</a></li>
  401. <li><a class="reference internal" href="#localized-text" id="id22">Localized Text</a></li>
  402. <li><a class="reference internal" href="#private-methods-and-variables" id="id23">Private Methods and Variables</a></li>
  403. <li><a class="reference internal" href="#php-errors" id="id24">PHP Errors</a></li>
  404. <li><a class="reference internal" href="#short-open-tags" id="id25">Short Open Tags</a></li>
  405. <li><a class="reference internal" href="#one-statement-per-line" id="id26">One Statement Per Line</a></li>
  406. <li><a class="reference internal" href="#strings" id="id27">Strings</a></li>
  407. <li><a class="reference internal" href="#sql-queries" id="id28">SQL Queries</a></li>
  408. <li><a class="reference internal" href="#default-function-arguments" id="id29">Default Function Arguments</a></li>
  409. </ul>
  410. </li>
  411. </ul>
  412. </div>
  413. <div class="section" id="file-format">
  414. <h2><a class="toc-backref" href="#id2">File Format</a><a class="headerlink" href="#file-format" title="Permalink to this headline">¶</a></h2>
  415. <p>Files should be saved with Unicode (UTF-8) encoding. The BOM should
  416. <em>not</em> be used. Unlike UTF-16 and UTF-32, there’s no byte order to
  417. indicate in a UTF-8 encoded file, and the BOM can have a negative side
  418. effect in PHP of sending output, preventing the application from being
  419. able to set its own headers. Unix line endings should be used (LF).</p>
  420. <p>Here is how to apply these settings in some of the more common text
  421. editors. Instructions for your text editor may vary; check your text
  422. editor’s documentation.</p>
  423. <div class="section" id="textmate">
  424. <h3><a class="toc-backref" href="#id3">TextMate</a><a class="headerlink" href="#textmate" title="Permalink to this headline">¶</a></h3>
  425. <ol class="arabic simple">
  426. <li>Open the Application Preferences</li>
  427. <li>Click Advanced, and then the “Saving” tab</li>
  428. <li>In “File Encoding”, select “UTF-8 (recommended)”</li>
  429. <li>In “Line Endings”, select “LF (recommended)”</li>
  430. <li><em>Optional:</em> Check “Use for existing files as well” if you wish to
  431. modify the line endings of files you open to your new preference.</li>
  432. </ol>
  433. </div>
  434. <div class="section" id="bbedit">
  435. <h3><a class="toc-backref" href="#id4">BBEdit</a><a class="headerlink" href="#bbedit" title="Permalink to this headline">¶</a></h3>
  436. <ol class="arabic simple">
  437. <li>Open the Application Preferences</li>
  438. <li>Select “Text Encodings” on the left.</li>
  439. <li>In “Default text encoding for new documents”, select “Unicode (UTF-8,
  440. no BOM)”</li>
  441. <li><em>Optional:</em> In “If file’s encoding can’t be guessed, use”, select
  442. “Unicode (UTF-8, no BOM)”</li>
  443. <li>Select “Text Files” on the left.</li>
  444. <li>In “Default line breaks”, select “Mac OS X and Unix (LF)”</li>
  445. </ol>
  446. </div>
  447. </div>
  448. <div class="section" id="php-closing-tag">
  449. <h2><a class="toc-backref" href="#id5">PHP Closing Tag</a><a class="headerlink" href="#php-closing-tag" title="Permalink to this headline">¶</a></h2>
  450. <p>The PHP closing tag on a PHP document <strong>?&gt;</strong> is optional to the PHP
  451. parser. However, if used, any whitespace following the closing tag,
  452. whether introduced by the developer, user, or an FTP application, can
  453. cause unwanted output, PHP errors, or if the latter are suppressed,
  454. blank pages. For this reason, all PHP files MUST OMIT the PHP closing
  455. tag and end with a single empty line instead.</p>
  456. </div>
  457. <div class="section" id="file-naming">
  458. <h2><a class="toc-backref" href="#id6">File Naming</a><a class="headerlink" href="#file-naming" title="Permalink to this headline">¶</a></h2>
  459. <p>Class files must be named in a Ucfirst-like manner, while any other file name
  460. (configurations, views, generic scripts, etc.) should be in all lowercase.</p>
  461. <p><strong>INCORRECT</strong>:</p>
  462. <div class="highlight-ci"><div class="highlight"><pre><span></span><span class="nx">somelibrary</span><span class="o">.</span><span class="nx">php</span>
  463. <span class="nx">someLibrary</span><span class="o">.</span><span class="nx">php</span>
  464. <span class="nx">SOMELIBRARY</span><span class="o">.</span><span class="nx">php</span>
  465. <span class="nx">Some_Library</span><span class="o">.</span><span class="nx">php</span>
  466. <span class="nx">Application_config</span><span class="o">.</span><span class="nx">php</span>
  467. <span class="nx">Application_Config</span><span class="o">.</span><span class="nx">php</span>
  468. <span class="nx">applicationConfig</span><span class="o">.</span><span class="nx">php</span>
  469. </pre></div>
  470. </div>
  471. <p><strong>CORRECT</strong>:</p>
  472. <div class="highlight-ci"><div class="highlight"><pre><span></span><span class="nx">Somelibrary</span><span class="o">.</span><span class="nx">php</span>
  473. <span class="nx">Some_library</span><span class="o">.</span><span class="nx">php</span>
  474. <span class="nx">applicationconfig</span><span class="o">.</span><span class="nx">php</span>
  475. <span class="nx">application_config</span><span class="o">.</span><span class="nx">php</span>
  476. </pre></div>
  477. </div>
  478. <p>Furthermore, class file names should match the name of the class itself.
  479. For example, if you have a class named <cite>Myclass</cite>, then its filename must
  480. be <strong>Myclass.php</strong>.</p>
  481. </div>
  482. <div class="section" id="class-and-method-naming">
  483. <h2><a class="toc-backref" href="#id7">Class and Method Naming</a><a class="headerlink" href="#class-and-method-naming" title="Permalink to this headline">¶</a></h2>
  484. <p>Class names should always start with an uppercase letter. Multiple words
  485. should be separated with an underscore, and not CamelCased.</p>
  486. <p><strong>INCORRECT</strong>:</p>
  487. <div class="highlight-ci"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">superclass</span>
  488. <span class="k">class</span> <span class="nc">SuperClass</span>
  489. </pre></div>
  490. </div>
  491. <p><strong>CORRECT</strong>:</p>
  492. <div class="highlight-ci"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">Super_class</span>
  493. </pre></div>
  494. </div>
  495. <div class="highlight-ci"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">Super_class</span> <span class="p">{</span>
  496. <span class="k">public</span> <span class="k">function</span> <span class="nf">__construct</span><span class="p">()</span>
  497. <span class="p">{</span>
  498. <span class="p">}</span>
  499. <span class="p">}</span>
  500. </pre></div>
  501. </div>
  502. <p>Class methods should be entirely lowercased and named to clearly
  503. indicate their function, preferably including a verb. Try to avoid
  504. overly long and verbose names. Multiple words should be separated
  505. with an underscore.</p>
  506. <p><strong>INCORRECT</strong>:</p>
  507. <div class="highlight-ci"><div class="highlight"><pre><span></span><span class="k">function</span> <span class="nf">fileproperties</span><span class="p">()</span> <span class="c1">// not descriptive and needs underscore separator</span>
  508. <span class="k">function</span> <span class="nf">fileProperties</span><span class="p">()</span> <span class="c1">// not descriptive and uses CamelCase</span>
  509. <span class="k">function</span> <span class="nf">getfileproperties</span><span class="p">()</span> <span class="c1">// Better! But still missing underscore separator</span>
  510. <span class="k">function</span> <span class="nf">getFileProperties</span><span class="p">()</span> <span class="c1">// uses CamelCase</span>
  511. <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>
  512. </pre></div>
  513. </div>
  514. <p><strong>CORRECT</strong>:</p>
  515. <div class="highlight-ci"><div class="highlight"><pre><span></span><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>
  516. </pre></div>
  517. </div>
  518. </div>
  519. <div class="section" id="variable-names">
  520. <h2><a class="toc-backref" href="#id8">Variable Names</a><a class="headerlink" href="#variable-names" title="Permalink to this headline">¶</a></h2>
  521. <p>The guidelines for variable naming are very similar to those used for
  522. class methods. Variables should contain only lowercase letters,
  523. use underscore separators, and be reasonably named to indicate their
  524. purpose and contents. Very short, non-word variables should only be used
  525. as iterators in for() loops.</p>
  526. <p><strong>INCORRECT</strong>:</p>
  527. <div class="highlight-ci"><div class="highlight"><pre><span></span><span class="nv">$j</span> <span class="o">=</span> <span class="s1">&#39;foo&#39;</span><span class="p">;</span> <span class="c1">// single letter variables should only be used in for() loops</span>
  528. <span class="nv">$Str</span> <span class="c1">// contains uppercase letters</span>
  529. <span class="nv">$bufferedText</span> <span class="c1">// uses CamelCasing, and could be shortened without losing semantic meaning</span>
  530. <span class="nv">$groupid</span> <span class="c1">// multiple words, needs underscore separator</span>
  531. <span class="nv">$name_of_last_city_used</span> <span class="c1">// too long</span>
  532. </pre></div>
  533. </div>
  534. <p><strong>CORRECT</strong>:</p>
  535. <div class="highlight-ci"><div class="highlight"><pre><span></span><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>
  536. <span class="nv">$str</span>
  537. <span class="nv">$buffer</span>
  538. <span class="nv">$group_id</span>
  539. <span class="nv">$last_city</span>
  540. </pre></div>
  541. </div>
  542. </div>
  543. <div class="section" id="commenting">
  544. <h2><a class="toc-backref" href="#id9">Commenting</a><a class="headerlink" href="#commenting" title="Permalink to this headline">¶</a></h2>
  545. <p>In general, code should be commented prolifically. It not only helps
  546. describe the flow and intent of the code for less experienced
  547. programmers, but can prove invaluable when returning to your own code
  548. months down the line. There is not a required format for comments, but
  549. the following are recommended.</p>
  550. <p><a class="reference external" href="http://manual.phpdoc.org/HTMLSmartyConverter/HandS/phpDocumentor/tutorial_phpDocumentor.howto.pkg.html#basics.docblock">DocBlock</a>
  551. style comments preceding class, method, and property declarations so they can be
  552. picked up by IDEs:</p>
  553. <div class="highlight-ci"><div class="highlight"><pre><span></span><span class="sd">/**</span>
  554. <span class="sd"> * Super Class</span>
  555. <span class="sd"> *</span>
  556. <span class="sd"> * @package Package Name</span>
  557. <span class="sd"> * @subpackage Subpackage</span>
  558. <span class="sd"> * @category Category</span>
  559. <span class="sd"> * @author Author Name</span>
  560. <span class="sd"> * @link http://example.com</span>
  561. <span class="sd"> */</span>
  562. <span class="k">class</span> <span class="nc">Super_class</span> <span class="p">{</span>
  563. </pre></div>
  564. </div>
  565. <div class="highlight-ci"><div class="highlight"><pre><span></span><span class="sd">/**</span>
  566. <span class="sd"> * Encodes string for use in XML</span>
  567. <span class="sd"> *</span>
  568. <span class="sd"> * @param string $str Input string</span>
  569. <span class="sd"> * @return string</span>
  570. <span class="sd"> */</span>
  571. <span class="k">function</span> <span class="nf">xml_encode</span><span class="p">(</span><span class="nv">$str</span><span class="p">)</span>
  572. </pre></div>
  573. </div>
  574. <div class="highlight-ci"><div class="highlight"><pre><span></span><span class="sd">/**</span>
  575. <span class="sd"> * Data for class manipulation</span>
  576. <span class="sd"> *</span>
  577. <span class="sd"> * @var array</span>
  578. <span class="sd"> */</span>
  579. <span class="k">public</span> <span class="nv">$data</span> <span class="o">=</span> <span class="k">array</span><span class="p">();</span>
  580. </pre></div>
  581. </div>
  582. <p>Use single line comments within code, leaving a blank line between large
  583. comment blocks and code.</p>
  584. <div class="highlight-ci"><div class="highlight"><pre><span></span><span class="c1">// break up the string by newlines</span>
  585. <span class="nv">$parts</span> <span class="o">=</span> <span class="nb">explode</span><span class="p">(</span><span class="s2">&quot;</span><span class="se">\n</span><span class="s2">&quot;</span><span class="p">,</span> <span class="nv">$str</span><span class="p">);</span>
  586. <span class="c1">// A longer comment that needs to give greater detail on what is</span>
  587. <span class="c1">// occurring and why can use multiple single-line comments. Try to</span>
  588. <span class="c1">// keep the width reasonable, around 70 characters is the easiest to</span>
  589. <span class="c1">// read. Don&#39;t hesitate to link to permanent external resources</span>
  590. <span class="c1">// that may provide greater detail:</span>
  591. <span class="c1">//</span>
  592. <span class="c1">// http://example.com/information_about_something/in_particular/</span>
  593. <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>
  594. </pre></div>
  595. </div>
  596. </div>
  597. <div class="section" id="constants">
  598. <h2><a class="toc-backref" href="#id10">Constants</a><a class="headerlink" href="#constants" title="Permalink to this headline">¶</a></h2>
  599. <p>Constants follow the same guidelines as do variables, except constants
  600. should always be fully uppercase. <em>Always use CodeIgniter constants when
  601. appropriate, i.e. SLASH, LD, RD, PATH_CACHE, etc.</em></p>
  602. <p><strong>INCORRECT</strong>:</p>
  603. <div class="highlight-ci"><div class="highlight"><pre><span></span><span class="nx">myConstant</span> <span class="c1">// missing underscore separator and not fully uppercase</span>
  604. <span class="nx">N</span> <span class="c1">// no single-letter constants</span>
  605. <span class="nx">S_C_VER</span> <span class="c1">// not descriptive</span>
  606. <span class="nv">$str</span> <span class="o">=</span> <span class="nb">str_replace</span><span class="p">(</span><span class="s1">&#39;{foo}&#39;</span><span class="p">,</span> <span class="s1">&#39;bar&#39;</span><span class="p">,</span> <span class="nv">$str</span><span class="p">);</span> <span class="c1">// should use LD and RD constants</span>
  607. </pre></div>
  608. </div>
  609. <p><strong>CORRECT</strong>:</p>
  610. <div class="highlight-ci"><div class="highlight"><pre><span></span><span class="nx">MY_CONSTANT</span>
  611. <span class="nx">NEWLINE</span>
  612. <span class="nx">SUPER_CLASS_VERSION</span>
  613. <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">&#39;foo&#39;</span><span class="o">.</span><span class="nx">RD</span><span class="p">,</span> <span class="s1">&#39;bar&#39;</span><span class="p">,</span> <span class="nv">$str</span><span class="p">);</span>
  614. </pre></div>
  615. </div>
  616. </div>
  617. <div class="section" id="true-false-and-null">
  618. <h2><a class="toc-backref" href="#id11">TRUE, FALSE, and NULL</a><a class="headerlink" href="#true-false-and-null" title="Permalink to this headline">¶</a></h2>
  619. <p><strong>TRUE</strong>, <strong>FALSE</strong>, and <strong>NULL</strong> keywords should always be fully
  620. uppercase.</p>
  621. <p><strong>INCORRECT</strong>:</p>
  622. <div class="highlight-ci"><div class="highlight"><pre><span></span><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>
  623. <span class="nv">$bar</span> <span class="o">=</span> <span class="k">false</span><span class="p">;</span>
  624. <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>
  625. </pre></div>
  626. </div>
  627. <p><strong>CORRECT</strong>:</p>
  628. <div class="highlight-ci"><div class="highlight"><pre><span></span><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>
  629. <span class="nv">$bar</span> <span class="o">=</span> <span class="k">FALSE</span><span class="p">;</span>
  630. <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>
  631. </pre></div>
  632. </div>
  633. </div>
  634. <div class="section" id="logical-operators">
  635. <h2><a class="toc-backref" href="#id12">Logical Operators</a><a class="headerlink" href="#logical-operators" title="Permalink to this headline">¶</a></h2>
  636. <p>Use of the <code class="docutils literal"><span class="pre">||</span></code> “or” comparison operator is discouraged, as its clarity
  637. on some output devices is low (looking like the number 11, for instance).
  638. <code class="docutils literal"><span class="pre">&amp;&amp;</span></code> is preferred over <code class="docutils literal"><span class="pre">AND</span></code> but either are acceptable, and a space should
  639. always precede and follow <code class="docutils literal"><span class="pre">!</span></code>.</p>
  640. <p><strong>INCORRECT</strong>:</p>
  641. <div class="highlight-ci"><div class="highlight"><pre><span></span><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>
  642. <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>
  643. <span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="nv">$foo</span><span class="p">)</span>
  644. <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>
  645. </pre></div>
  646. </div>
  647. <p><strong>CORRECT</strong>:</p>
  648. <div class="highlight-ci"><div class="highlight"><pre><span></span><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>
  649. <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>
  650. <span class="k">if</span> <span class="p">(</span> <span class="o">!</span> <span class="nv">$foo</span><span class="p">)</span>
  651. <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>
  652. </pre></div>
  653. </div>
  654. </div>
  655. <div class="section" id="comparing-return-values-and-typecasting">
  656. <h2><a class="toc-backref" href="#id13">Comparing Return Values and Typecasting</a><a class="headerlink" href="#comparing-return-values-and-typecasting" title="Permalink to this headline">¶</a></h2>
  657. <p>Some PHP functions return FALSE on failure, but may also have a valid
  658. return value of “” or 0, which would evaluate to FALSE in loose
  659. comparisons. Be explicit by comparing the variable type when using these
  660. return values in conditionals to ensure the return value is indeed what
  661. you expect, and not a value that has an equivalent loose-type
  662. evaluation.</p>
  663. <p>Use the same stringency in returning and checking your own variables.
  664. Use <strong>===</strong> and <strong>!==</strong> as necessary.</p>
  665. <p><strong>INCORRECT</strong>:</p>
  666. <div class="highlight-ci"><div class="highlight"><pre><span></span><span class="c1">// If &#39;foo&#39; is at the beginning of the string, strpos will return a 0,</span>
  667. <span class="c1">// resulting in this conditional evaluating as TRUE</span>
  668. <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">&#39;foo&#39;</span><span class="p">)</span> <span class="o">==</span> <span class="k">FALSE</span><span class="p">)</span>
  669. </pre></div>
  670. </div>
  671. <p><strong>CORRECT</strong>:</p>
  672. <div class="highlight-ci"><div class="highlight"><pre><span></span><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">&#39;foo&#39;</span><span class="p">)</span> <span class="o">===</span> <span class="k">FALSE</span><span class="p">)</span>
  673. </pre></div>
  674. </div>
  675. <p><strong>INCORRECT</strong>:</p>
  676. <div class="highlight-ci"><div class="highlight"><pre><span></span><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">&quot;&quot;</span><span class="p">)</span>
  677. <span class="p">{</span>
  678. <span class="k">if</span> <span class="p">(</span><span class="nv">$str</span> <span class="o">==</span> <span class="s2">&quot;&quot;</span><span class="p">)</span> <span class="c1">// uh-oh! What if FALSE or the integer 0 is passed as an argument?</span>
  679. <span class="p">{</span>
  680. <span class="p">}</span>
  681. <span class="p">}</span>
  682. </pre></div>
  683. </div>
  684. <p><strong>CORRECT</strong>:</p>
  685. <div class="highlight-ci"><div class="highlight"><pre><span></span><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">&quot;&quot;</span><span class="p">)</span>
  686. <span class="p">{</span>
  687. <span class="k">if</span> <span class="p">(</span><span class="nv">$str</span> <span class="o">===</span> <span class="s2">&quot;&quot;</span><span class="p">)</span>
  688. <span class="p">{</span>
  689. <span class="p">}</span>
  690. <span class="p">}</span>
  691. </pre></div>
  692. </div>
  693. <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>,
  694. which can be quite useful. Typecasting has a slightly different effect
  695. which may be desirable. When casting a variable as a string, for
  696. instance, NULL and boolean FALSE variables become empty strings, 0 (and
  697. other numbers) become strings of digits, and boolean TRUE becomes “1”:</p>
  698. <div class="highlight-ci"><div class="highlight"><pre><span></span><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>
  699. </pre></div>
  700. </div>
  701. </div>
  702. <div class="section" id="debugging-code">
  703. <h2><a class="toc-backref" href="#id14">Debugging Code</a><a class="headerlink" href="#debugging-code" title="Permalink to this headline">¶</a></h2>
  704. <p>Do not leave debugging code in your submissions, even when commented out.
  705. Things such as <code class="docutils literal"><span class="pre">var_dump()</span></code>, <code class="docutils literal"><span class="pre">print_r()</span></code>, <code class="docutils literal"><span class="pre">die()</span></code>/<code class="docutils literal"><span class="pre">exit()</span></code> should not be included
  706. in your code unless it serves a specific purpose other than debugging.</p>
  707. </div>
  708. <div class="section" id="whitespace-in-files">
  709. <h2><a class="toc-backref" href="#id15">Whitespace in Files</a><a class="headerlink" href="#whitespace-in-files" title="Permalink to this headline">¶</a></h2>
  710. <p>No whitespace can precede the opening PHP tag or follow the closing PHP
  711. tag. Output is buffered, so whitespace in your files can cause output to
  712. begin before CodeIgniter outputs its content, leading to errors and an
  713. inability for CodeIgniter to send proper headers.</p>
  714. </div>
  715. <div class="section" id="compatibility">
  716. <h2><a class="toc-backref" href="#id16">Compatibility</a><a class="headerlink" href="#compatibility" title="Permalink to this headline">¶</a></h2>
  717. <p>CodeIgniter recommends PHP 5.6 or newer to be used, but it should be
  718. compatible with PHP 5.3.7. Your code must either be compatible with this
  719. requirement, provide a suitable fallback, or be an optional feature that
  720. dies quietly without affecting a user’s application.</p>
  721. <p>Additionally, do not use PHP functions that require non-default libraries
  722. to be installed unless your code contains an alternative method when the
  723. function is not available.</p>
  724. </div>
  725. <div class="section" id="one-file-per-class">
  726. <h2><a class="toc-backref" href="#id17">One File per Class</a><a class="headerlink" href="#one-file-per-class" title="Permalink to this headline">¶</a></h2>
  727. <p>Use separate files for each class, unless the classes are <em>closely related</em>.
  728. An example of a CodeIgniter file that contains multiple classes is the
  729. Xmlrpc library file.</p>
  730. </div>
  731. <div class="section" id="whitespace">
  732. <h2><a class="toc-backref" href="#id18">Whitespace</a><a class="headerlink" href="#whitespace" title="Permalink to this headline">¶</a></h2>
  733. <p>Use tabs for whitespace in your code, not spaces. This may seem like a
  734. small thing, but using tabs instead of whitespace allows the developer
  735. looking at your code to have indentation at levels that they prefer and
  736. customize in whatever application they use. And as a side benefit, it
  737. results in (slightly) more compact files, storing one tab character
  738. versus, say, four space characters.</p>
  739. </div>
  740. <div class="section" id="line-breaks">
  741. <h2><a class="toc-backref" href="#id19">Line Breaks</a><a class="headerlink" href="#line-breaks" title="Permalink to this headline">¶</a></h2>
  742. <p>Files must be saved with Unix line breaks. This is more of an issue for
  743. developers who work in Windows, but in any case ensure that your text
  744. editor is setup to save files with Unix line breaks.</p>
  745. </div>
  746. <div class="section" id="code-indenting">
  747. <h2><a class="toc-backref" href="#id20">Code Indenting</a><a class="headerlink" href="#code-indenting" title="Permalink to this headline">¶</a></h2>
  748. <p>Use Allman style indenting. With the exception of Class declarations,
  749. braces are always placed on a line by themselves, and indented at the
  750. same level as the control statement that “owns” them.</p>
  751. <p><strong>INCORRECT</strong>:</p>
  752. <div class="highlight-ci"><div class="highlight"><pre><span></span><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>
  753. <span class="c1">// ...</span>
  754. <span class="p">}</span>
  755. <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>
  756. <span class="c1">// ...</span>
  757. <span class="p">}</span>
  758. <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>
  759. <span class="c1">// ...</span>
  760. <span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
  761. <span class="c1">// ...</span>
  762. <span class="p">}</span>
  763. <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>
  764. <span class="p">{</span>
  765. <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>
  766. <span class="p">{</span>
  767. <span class="c1">// ...</span>
  768. <span class="p">}</span>
  769. <span class="p">}</span>
  770. <span class="k">try</span> <span class="p">{</span>
  771. <span class="c1">// ...</span>
  772. <span class="p">}</span>
  773. <span class="k">catch</span><span class="p">()</span> <span class="p">{</span>
  774. <span class="c1">// ...</span>
  775. <span class="p">}</span>
  776. </pre></div>
  777. </div>
  778. <p><strong>CORRECT</strong>:</p>
  779. <div class="highlight-ci"><div class="highlight"><pre><span></span><span class="k">function</span> <span class="nf">foo</span><span class="p">(</span><span class="nv">$bar</span><span class="p">)</span>
  780. <span class="p">{</span>
  781. <span class="c1">// ...</span>
  782. <span class="p">}</span>
  783. <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>
  784. <span class="p">{</span>
  785. <span class="c1">// ...</span>
  786. <span class="p">}</span>
  787. <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>
  788. <span class="p">{</span>
  789. <span class="c1">// ...</span>
  790. <span class="p">}</span>
  791. <span class="k">else</span>
  792. <span class="p">{</span>
  793. <span class="c1">// ...</span>
  794. <span class="p">}</span>
  795. <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>
  796. <span class="p">{</span>
  797. <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>
  798. <span class="p">{</span>
  799. <span class="c1">// ...</span>
  800. <span class="p">}</span>
  801. <span class="p">}</span>
  802. <span class="k">try</span>
  803. <span class="p">{</span>
  804. <span class="c1">// ...</span>
  805. <span class="p">}</span>
  806. <span class="k">catch</span><span class="p">()</span>
  807. <span class="p">{</span>
  808. <span class="c1">// ...</span>
  809. <span class="p">}</span>
  810. </pre></div>
  811. </div>
  812. </div>
  813. <div class="section" id="bracket-and-parenthetic-spacing">
  814. <h2><a class="toc-backref" href="#id21">Bracket and Parenthetic Spacing</a><a class="headerlink" href="#bracket-and-parenthetic-spacing" title="Permalink to this headline">¶</a></h2>
  815. <p>In general, parenthesis and brackets should not use any additional
  816. spaces. The exception is that a space should always follow PHP control
  817. structures that accept arguments with parenthesis (declare, do-while,
  818. elseif, for, foreach, if, switch, while), to help distinguish them from
  819. functions and increase readability.</p>
  820. <p><strong>INCORRECT</strong>:</p>
  821. <div class="highlight-ci"><div class="highlight"><pre><span></span><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">&#39;foo&#39;</span><span class="p">;</span>
  822. </pre></div>
  823. </div>
  824. <p><strong>CORRECT</strong>:</p>
  825. <div class="highlight-ci"><div class="highlight"><pre><span></span><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">&#39;foo&#39;</span><span class="p">;</span> <span class="c1">// no spaces around array keys</span>
  826. </pre></div>
  827. </div>
  828. <p><strong>INCORRECT</strong>:</p>
  829. <div class="highlight-ci"><div class="highlight"><pre><span></span><span class="k">function</span> <span class="nf">foo</span> <span class="p">(</span> <span class="nv">$bar</span> <span class="p">)</span>
  830. <span class="p">{</span>
  831. <span class="p">}</span>
  832. </pre></div>
  833. </div>
  834. <p><strong>CORRECT</strong>:</p>
  835. <div class="highlight-ci"><div class="highlight"><pre><span></span><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>
  836. <span class="p">{</span>
  837. <span class="p">}</span>
  838. </pre></div>
  839. </div>
  840. <p><strong>INCORRECT</strong>:</p>
  841. <div class="highlight-ci"><div class="highlight"><pre><span></span><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>
  842. </pre></div>
  843. </div>
  844. <p><strong>CORRECT</strong>:</p>
  845. <div class="highlight-ci"><div class="highlight"><pre><span></span><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>
  846. </pre></div>
  847. </div>
  848. </div>
  849. <div class="section" id="localized-text">
  850. <h2><a class="toc-backref" href="#id22">Localized Text</a><a class="headerlink" href="#localized-text" title="Permalink to this headline">¶</a></h2>
  851. <p>CodeIgniter libraries should take advantage of corresponding language files
  852. whenever possible.</p>
  853. <p><strong>INCORRECT</strong>:</p>
  854. <div class="highlight-ci"><div class="highlight"><pre><span></span><span class="k">return</span> <span class="s2">&quot;Invalid Selection&quot;</span><span class="p">;</span>
  855. </pre></div>
  856. </div>
  857. <p><strong>CORRECT</strong>:</p>
  858. <div class="highlight-ci"><div class="highlight"><pre><span></span><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">&#39;invalid_selection&#39;</span><span class="p">);</span>
  859. </pre></div>
  860. </div>
  861. </div>
  862. <div class="section" id="private-methods-and-variables">
  863. <h2><a class="toc-backref" href="#id23">Private Methods and Variables</a><a class="headerlink" href="#private-methods-and-variables" title="Permalink to this headline">¶</a></h2>
  864. <p>Methods and variables that are only accessed internally,
  865. such as utility and helper functions that your public methods use for
  866. code abstraction, should be prefixed with an underscore.</p>
  867. <div class="highlight-ci"><div class="highlight"><pre><span></span><span class="k">public</span> <span class="k">function</span> <span class="nf">convert_text</span><span class="p">()</span>
  868. <span class="k">private</span> <span class="k">function</span> <span class="nf">_convert_text</span><span class="p">()</span>
  869. </pre></div>
  870. </div>
  871. </div>
  872. <div class="section" id="php-errors">
  873. <h2><a class="toc-backref" href="#id24">PHP Errors</a><a class="headerlink" href="#php-errors" title="Permalink to this headline">¶</a></h2>
  874. <p>Code must run error free and not rely on warnings and notices to be
  875. hidden to meet this requirement. For instance, never access a variable
  876. that you did not set yourself (such as <code class="docutils literal"><span class="pre">$_POST</span></code> array keys) without first
  877. checking to see that it <code class="docutils literal"><span class="pre">isset()</span></code>.</p>
  878. <p>Make sure that your dev environment has error reporting enabled
  879. for ALL users, and that display_errors is enabled in the PHP
  880. environment. You can check this setting with:</p>
  881. <div class="highlight-ci"><div class="highlight"><pre><span></span><span class="k">if</span> <span class="p">(</span><span class="nb">ini_get</span><span class="p">(</span><span class="s1">&#39;display_errors&#39;</span><span class="p">)</span> <span class="o">==</span> <span class="mi">1</span><span class="p">)</span>
  882. <span class="p">{</span>
  883. <span class="k">exit</span> <span class="s2">&quot;Enabled&quot;</span><span class="p">;</span>
  884. <span class="p">}</span>
  885. </pre></div>
  886. </div>
  887. <p>On some servers where <em>display_errors</em> is disabled, and you do not have
  888. the ability to change this in the php.ini, you can often enable it with:</p>
  889. <div class="highlight-ci"><div class="highlight"><pre><span></span><span class="nb">ini_set</span><span class="p">(</span><span class="s1">&#39;display_errors&#39;</span><span class="p">,</span> <span class="mi">1</span><span class="p">);</span>
  890. </pre></div>
  891. </div>
  892. <div class="admonition note">
  893. <p class="first admonition-title">Note</p>
  894. <p class="last">Setting the <a class="reference external" href="http://php.net/manual/en/errorfunc.configuration.php#ini.display-errors">display_errors</a>
  895. setting with <code class="docutils literal"><span class="pre">ini_set()</span></code> at runtime is not identical to having
  896. it enabled in the PHP environment. Namely, it will not have any
  897. effect if the script has fatal errors.</p>
  898. </div>
  899. </div>
  900. <div class="section" id="short-open-tags">
  901. <h2><a class="toc-backref" href="#id25">Short Open Tags</a><a class="headerlink" href="#short-open-tags" title="Permalink to this headline">¶</a></h2>
  902. <p>Always use full PHP opening tags, in case a server does not have
  903. <em>short_open_tag</em> enabled.</p>
  904. <p><strong>INCORRECT</strong>:</p>
  905. <div class="highlight-ci"><div class="highlight"><pre><span></span><span class="o">&lt;?</span> <span class="k">echo</span> <span class="nv">$foo</span><span class="p">;</span> <span class="cp">?&gt;</span>
  906. <span class="cp">&lt;?</span><span class="o">=</span><span class="nv">$foo</span><span class="cp">?&gt;</span>
  907. </pre></div>
  908. </div>
  909. <p><strong>CORRECT</strong>:</p>
  910. <div class="highlight-ci"><div class="highlight"><pre><span></span><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>
  911. </pre></div>
  912. </div>
  913. <div class="admonition note">
  914. <p class="first admonition-title">Note</p>
  915. <p class="last">PHP 5.4 will always have the <strong>&lt;?=</strong> tag available.</p>
  916. </div>
  917. </div>
  918. <div class="section" id="one-statement-per-line">
  919. <h2><a class="toc-backref" href="#id26">One Statement Per Line</a><a class="headerlink" href="#one-statement-per-line" title="Permalink to this headline">¶</a></h2>
  920. <p>Never combine statements on one line.</p>
  921. <p><strong>INCORRECT</strong>:</p>
  922. <div class="highlight-ci"><div class="highlight"><pre><span></span><span class="nv">$foo</span> <span class="o">=</span> <span class="s1">&#39;this&#39;</span><span class="p">;</span> <span class="nv">$bar</span> <span class="o">=</span> <span class="s1">&#39;that&#39;</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>
  923. </pre></div>
  924. </div>
  925. <p><strong>CORRECT</strong>:</p>
  926. <div class="highlight-ci"><div class="highlight"><pre><span></span><span class="nv">$foo</span> <span class="o">=</span> <span class="s1">&#39;this&#39;</span><span class="p">;</span>
  927. <span class="nv">$bar</span> <span class="o">=</span> <span class="s1">&#39;that&#39;</span><span class="p">;</span>
  928. <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>
  929. </pre></div>
  930. </div>
  931. </div>
  932. <div class="section" id="strings">
  933. <h2><a class="toc-backref" href="#id27">Strings</a><a class="headerlink" href="#strings" title="Permalink to this headline">¶</a></h2>
  934. <p>Always use single quoted strings unless you need variables parsed, and
  935. in cases where you do need variables parsed, use braces to prevent
  936. greedy token parsing. You may also use double-quoted strings if the
  937. string contains single quotes, so you do not have to use escape
  938. characters.</p>
  939. <p><strong>INCORRECT</strong>:</p>
  940. <div class="highlight-ci"><div class="highlight"><pre><span></span><span class="s2">&quot;My String&quot;</span> <span class="c1">// no variable parsing, so no use for double quotes</span>
  941. <span class="s2">&quot;My string </span><span class="si">$foo</span><span class="s2">&quot;</span> <span class="c1">// needs braces</span>
  942. <span class="s1">&#39;SELECT foo FROM bar WHERE baz = \&#39;bag\&#39;&#39;</span> <span class="c1">// ugly</span>
  943. </pre></div>
  944. </div>
  945. <p><strong>CORRECT</strong>:</p>
  946. <div class="highlight-ci"><div class="highlight"><pre><span></span><span class="s1">&#39;My String&#39;</span>
  947. <span class="s2">&quot;My string </span><span class="si">{</span><span class="nv">$foo</span><span class="si">}</span><span class="s2">&quot;</span>
  948. <span class="s2">&quot;SELECT foo FROM bar WHERE baz = &#39;bag&#39;&quot;</span>
  949. </pre></div>
  950. </div>
  951. </div>
  952. <div class="section" id="sql-queries">
  953. <h2><a class="toc-backref" href="#id28">SQL Queries</a><a class="headerlink" href="#sql-queries" title="Permalink to this headline">¶</a></h2>
  954. <p>SQL keywords are always capitalized: SELECT, INSERT, UPDATE, WHERE,
  955. AS, JOIN, ON, IN, etc.</p>
  956. <p>Break up long queries into multiple lines for legibility, preferably
  957. breaking for each clause.</p>
  958. <p><strong>INCORRECT</strong>:</p>
  959. <div class="highlight-ci"><div class="highlight"><pre><span></span><span class="c1">// keywords are lowercase and query is too long for</span>
  960. <span class="c1">// a single line (... indicates continuation of line)</span>
  961. <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">&quot;select foo, bar, baz, foofoo, foobar as raboof, foobaz from exp_pre_email_addresses</span>
  962. <span class="s2">...where foo != &#39;oof&#39; and baz != &#39;zab&#39; order by foobaz limit 5, 100&quot;</span><span class="p">);</span>
  963. </pre></div>
  964. </div>
  965. <p><strong>CORRECT</strong>:</p>
  966. <div class="highlight-ci"><div class="highlight"><pre><span></span><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">&quot;SELECT foo, bar, baz, foofoo, foobar AS raboof, foobaz</span>
  967. <span class="s2"> FROM exp_pre_email_addresses</span>
  968. <span class="s2"> WHERE foo != &#39;oof&#39;</span>
  969. <span class="s2"> AND baz != &#39;zab&#39;</span>
  970. <span class="s2"> ORDER BY foobaz</span>
  971. <span class="s2"> LIMIT 5, 100&quot;</span><span class="p">);</span>
  972. </pre></div>
  973. </div>
  974. </div>
  975. <div class="section" id="default-function-arguments">
  976. <h2><a class="toc-backref" href="#id29">Default Function Arguments</a><a class="headerlink" href="#default-function-arguments" title="Permalink to this headline">¶</a></h2>
  977. <p>Whenever appropriate, provide function argument defaults, which helps
  978. prevent PHP errors with mistaken calls and provides common fallback
  979. values which can save a few lines of code. Example:</p>
  980. <div class="highlight-ci"><div class="highlight"><pre><span></span><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">&#39;&#39;</span><span class="p">,</span> <span class="nv">$baz</span> <span class="o">=</span> <span class="k">FALSE</span><span class="p">)</span>
  981. </pre></div>
  982. </div>
  983. </div>
  984. </div>
  985. </div>
  986. <footer>
  987. <div class="rst-footer-buttons" role="navigation" aria-label="footer navigation">
  988. <a href="../libraries/index.html" class="btn btn-neutral float-right" title="Libraries">Next <span class="fa fa-arrow-circle-right"></span></a>
  989. <a href="security.html" class="btn btn-neutral" title="Security"><span class="fa fa-arrow-circle-left"></span> Previous</a>
  990. </div>
  991. <hr/>
  992. <div role="contentinfo">
  993. <p>
  994. &copy; Copyright 2014 - 2019, British Columbia Institute of Technology.
  995. Last updated on Sep 19, 2019.
  996. </p>
  997. </div>
  998. Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/snide/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>.
  999. </footer>
  1000. </div>
  1001. </div>
  1002. </section>
  1003. </div>
  1004. <script type="text/javascript">
  1005. var DOCUMENTATION_OPTIONS = {
  1006. URL_ROOT:'../',
  1007. VERSION:'3.1.11',
  1008. COLLAPSE_INDEX:false,
  1009. FILE_SUFFIX:'.html',
  1010. HAS_SOURCE: false
  1011. };
  1012. </script>
  1013. <script type="text/javascript" src="../_static/jquery.js"></script>
  1014. <script type="text/javascript" src="../_static/underscore.js"></script>
  1015. <script type="text/javascript" src="../_static/doctools.js"></script>
  1016. <script type="text/javascript" src="../_static/js/theme.js"></script>
  1017. <script type="text/javascript">
  1018. jQuery(function () {
  1019. SphinxRtdTheme.StickyNav.enable();
  1020. });
  1021. </script>
  1022. </body>
  1023. </html>