|
From: Meik S. <acy...@us...> - 2007-09-22 18:13:09
|
Update of /cvsroot/phpbb/phpBB2/docs In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv3607 Modified Files: CHANGELOG.html FAQ.html INSTALL.html README.html auth_api.html coding-guidelines.html Added Files: bg_header.gif corners_left.gif corners_left.png corners_right.gif corners_right.png site_logo.gif stylesheet.css Removed Files: header_bg.jpg header_left.jpg Log Message: updated documents to prosilver style --- NEW FILE: site_logo.gif --- (This appears to be a binary file; contents omitted.) Index: coding-guidelines.html =================================================================== RCS file: /cvsroot/phpbb/phpBB2/docs/coding-guidelines.html,v retrieving revision 1.29 retrieving revision 1.30 diff -C2 -d -r1.29 -r1.30 *** coding-guidelines.html 13 Sep 2007 15:00:33 -0000 1.29 --- coding-guidelines.html 22 Sep 2007 18:13:08 -0000 1.30 *************** *** 1,193 **** ! <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ! "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> ! <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> - <title>Coding Guidelines</title> - <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> - <meta http-equiv="Content-Style-Type" content="text/css" /> - <meta name="resource-type" content="document" /> - <meta name="description" lang="en" content="Olympus coding guidelines document" /> - <meta name="keywords" lang="en" content="" /> - <meta name="author" content="phpBB Group" /> - <meta name="copyright" content="phpBB Group" /> - <meta name="MSSmartTagsPreventParsing" content="true" /> - - <style type="text/css"> - <!-- - - /* The original "subSilver" theme for phpBB2 Created by subBlue design :: http://www.subBlue.com */ - body { - background-color: white; - font-family: Verdana, Arial, Helvetica, sans-serif; - margin: 0; - border: 0; - padding: 0; - } - - img { - border: 0; - } - - p { - font-size: 8pt; - } - - hr { - height: 0; - border: solid #D1D7DC 0; - border-top-width: 1px; - } - - #title, h1 { - font: bold 18pt 'Trebuchet MS', Verdana, sans-serif; - text-decoration: none; - line-height: 120%; - } - - h2 { - font: bold 12pt Arial, Helvetica, sans-serif; - text-decoration: none; - line-height: 120%; - } - - h3 { - font: bold 10pt Arial, Helvetica, sans-serif; - text-decoration: none; - line-height: 120%; - } - - .paragraph { - margin-left: 20px; - } - - .paragraph table { - font-size: 8pt; - border-collapse: collapse; - border: 1px solid #006699; - } - - .paragraph table caption { - display: none; - } - - .paragraph table thead { - background-color: #D1D7DC; - } - - .paragraph table td, .paragraph table th { - border: 1px solid #006699; - padding: 0.5em; - } - - .paragraph table td dl { - margin: 0; - padding: 0; - } - - .paragraph table td dl dt { - float: left; - clear: both; - margin-right: 1em; - } - - /* Structure */ - #logo { - background: #fff url(header_bg.jpg) repeat-x top right; - height: 60px; - } - - #title { - color: #12749b; - float: right; - margin: 10px 10px 0; - } - - #main { - margin-left: 25px; - margin-right: 25px; - } - - .good { - color: green; - } - - .bad { - color: red; - } - - #footer { - margin-left: 75px; - font-size: 70%; - color: #006600; - } - - code { - color: #006600; - font-weight: normal; - font-family: 'Courier New', monospace; - border-color: #D1D7DC; - border-width: 1px; - border-style: solid; - background-color: #FAFAFA; - } - - .indent p { - padding-left: 20px; - font-size: 90%; - } - - /* Anchors */ - a { - font-size: 70%; - } - - a:link, a:active, a:visited { - color: #006699; - text-decoration: none; - } - - a:hover { - color: #DD6900; - text-decoration: underline; - } ! a.nav { ! color: #006699; ! text-decoration: none; ! } ! ! a.nav:hover { ! text-decoration: underline; ! } ! ! p a { ! font-size: 100%; ! } ! .menu { ! font-size: 80%; ! } ! .menu li a { ! font-size: 100%; ! } ! .comment { ! color: green; ! } ! //--> ! </style> ! </head> ! <body> ! <div id="logo"> ! <div id="title">Coding Guidelines</div> ! <img src="header_left.jpg" alt="phpBB Logo" /> ! </div> ! <a name="top"></a><div id="main"> <!-- BEGIN DOCUMENT --> --- 1,43 ---- ! <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> ! <html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" lang="en" xml:lang="en"> <head> ! <meta http-equiv="content-type" content="text/html; charset=utf-8" /> ! <meta http-equiv="content-style-type" content="text/css" /> ! <meta http-equiv="content-language" content="en" /> ! <meta http-equiv="imagetoolbar" content="no" /> ! <meta name="resource-type" content="document" /> ! <meta name="distribution" content="global" /> ! <meta name="copyright" content="2007 phpBB Group" /> ! <meta name="keywords" content="" /> ! <meta name="description" content="Olympus coding guidelines document" /> ! <title>phpBB3 • Coding Guidelines</title> ! <link href="stylesheet.css" rel="stylesheet" type="text/css" media="screen, projection" /> ! </head> ! <body id="phpbb" class="section-docs"> ! <div id="wrap"> ! <a id="top" name="top" accesskey="t"></a> ! <div id="page-header"> ! <div class="headerbar"> ! <div class="inner"><span class="corners-top"><span></span></span> ! <div id="doc-description"> ! <a href="../index.php" id="logo"><img src="site_logo.gif" alt="" /></a> ! <h1>Coding Guidelines</h1> ! <p>Olympus coding guidelines document</p> ! <p style="display: none;"><a href="#start_here">Skip</a></p> ! </div> ! <span class="corners-bottom"><span></span></span></div> ! </div> ! </div> ! <a name="start_here"></a> ! <div id="page-body"> <!-- BEGIN DOCUMENT --> *************** *** 197,203 **** <h1>Coding Guidelines</h1> ! <ol class="menu"> <li><a href="#defaults">Defaults</a> ! <ol type="i"> <li><a href="#editorsettings">Editor Settings</a></li> <li><a href="#fileheader">File Header</a></li> --- 47,58 ---- <h1>Coding Guidelines</h1> ! <div class="paragraph menu"> ! <div class="inner"><span class="corners-top"><span></span></span> ! ! <div class="content"> ! ! <ol> <li><a href="#defaults">Defaults</a> ! <ol style="list-style-type: lower-roman;"> <li><a href="#editorsettings">Editor Settings</a></li> <li><a href="#fileheader">File Header</a></li> *************** *** 206,210 **** </li> <li><a href="#code">Code Layout/Guidelines</a> ! <ol type="i"> <li><a href="#namingvars">Variable/Function Naming</a></li> <li><a href="#codelayout">Code Layout</a></li> --- 61,65 ---- </li> <li><a href="#code">Code Layout/Guidelines</a> ! <ol style="list-style-type: lower-roman;"> <li><a href="#namingvars">Variable/Function Naming</a></li> <li><a href="#codelayout">Code Layout</a></li> *************** *** 218,222 **** <li><a href="#charsets">Character Sets and Encodings</a></li> <li><a href="#translation">Translation (<abbr title="Internationalisation">i18n</abbr>/<abbr title="Localisation">L10n</abbr>) Guidelines</a> ! <ol type="i"> <li><a href="#standardisation">Standardisation</a></li> <li><a href="#otherconsiderations">Other considerations</a></li> --- 73,77 ---- <li><a href="#charsets">Character Sets and Encodings</a></li> <li><a href="#translation">Translation (<abbr title="Internationalisation">i18n</abbr>/<abbr title="Localisation">L10n</abbr>) Guidelines</a> ! <ol style="list-style-type: lower-roman;"> <li><a href="#standardisation">Standardisation</a></li> <li><a href="#otherconsiderations">Other considerations</a></li> *************** *** 228,247 **** </ol> ! <hr /> ! <a name="defaults"></a><h1>1. Defaults</h1> - <a name="editorsettings"></a><b>1.i. Editor Settings</b> - <br /><br /> <div class="paragraph"> ! <h3>Tabs vs Spaces:</h3> ! <p>In order to make this as simple as possible, we will be using tabs, not spaces. We enforce 4 (four) spaces for one tab - therefore you need to set your tab width within your editor to 4 spaces. Make sure that when you <b>save</b> the file, it's saving tabs and not spaces. This way, we can each have the code be displayed the way we like it, without breaking the layout of the actual files.</p> <p>Tabs in front of lines are no problem, but having them within the text can be a problem if you do not set it to the amount of spaces every one of us uses. Here is a short example of how it should look like:</p> ! <blockquote><pre> {TAB}$mode{TAB}{TAB}= request_var('mode', ''); {TAB}$search_id{TAB}= request_var('search_id', ''); ! </pre></blockquote> <p>If entered with tabs (replace the {TAB}) both equal signs need to be on the same column.</p> --- 83,110 ---- </ol> ! </div> ! <span class="corners-bottom"><span></span></span></div> ! </div> ! ! <hr /> ! ! <a name="defaults"></a><h2>1. Defaults</h2> <div class="paragraph"> + <div class="inner"><span class="corners-top"><span></span></span> ! <div class="content"> ! ! <a name="editorsettings"></a><h3>1.i. Editor Settings</h3> ! ! <h4>Tabs vs Spaces:</h4> ! <p>In order to make this as simple as possible, we will be using tabs, not spaces. We enforce 4 (four) spaces for one tab - therefore you need to set your tab width within your editor to 4 spaces. Make sure that when you <strong>save</strong> the file, it's saving tabs and not spaces. This way, we can each have the code be displayed the way we like it, without breaking the layout of the actual files.</p> <p>Tabs in front of lines are no problem, but having them within the text can be a problem if you do not set it to the amount of spaces every one of us uses. Here is a short example of how it should look like:</p> ! <div class="codebox"><pre> {TAB}$mode{TAB}{TAB}= request_var('mode', ''); {TAB}$search_id{TAB}= request_var('search_id', ''); ! </pre></div> <p>If entered with tabs (replace the {TAB}) both equal signs need to be on the same column.</p> *************** *** 250,283 **** <p>Ensure that your editor is saving files in the UNIX format. This means lines are terminated with a newline, not with a CR/LF combo as they are on Win32, or whatever the Mac uses. Any decent editor should be able to do this, but it might not always be the default. Know your editor. If you want advice on Windows text editors, just ask one of the developers. Some of them do their editing on Win32.</p> ! </div> ! <a href="#top">Top</a> ! <br /><br /> ! ! ! <a name="fileheader"></a><b>1.ii. File Header</b> ! <br /><br /> ! <div class="paragraph"> ! <h3>Standard header for new files:</h3> <p>This template of the header must be included at the start of all phpBB files: </p> ! <blockquote><pre> /** * * @package {PACKAGENAME} * @version $Id: $ ! * @copyright (c) 2006 phpBB Group * @license http://opensource.org/licenses/gpl-license.php GNU Public License * */ ! </pre></blockquote> <p>Please see the <a href="#locations">File Locations section</a> for the correct package name.</p> ! <h3>Files containing inline code:</h3> <p>For those files you have to put an empty comment directly after the header to prevent the documentor assigning the header to the first code element found.</p> ! <blockquote><pre> /** * {HEADER} --- 113,139 ---- <p>Ensure that your editor is saving files in the UNIX format. This means lines are terminated with a newline, not with a CR/LF combo as they are on Win32, or whatever the Mac uses. Any decent editor should be able to do this, but it might not always be the default. Know your editor. If you want advice on Windows text editors, just ask one of the developers. Some of them do their editing on Win32.</p> ! <a name="fileheader"></a><h3>1.ii. File Header</h3> ! <h4>Standard header for new files:</h4> <p>This template of the header must be included at the start of all phpBB files: </p> ! <div class="codebox"><pre> /** * * @package {PACKAGENAME} * @version $Id: $ ! * @copyright (c) 2007 phpBB Group * @license http://opensource.org/licenses/gpl-license.php GNU Public License * */ ! </pre></div> <p>Please see the <a href="#locations">File Locations section</a> for the correct package name.</p> ! <h4>Files containing inline code:</h4> <p>For those files you have to put an empty comment directly after the header to prevent the documentor assigning the header to the first code element found.</p> ! <div class="codebox"><pre> /** * {HEADER} *************** *** 287,305 **** */ {CODE} ! </pre></blockquote> ! <h3>Files containing only functions:</h3> <p>Do not forget to comment the functions (especially the first function following the header). Each function should have at least a comment of what this function does. For more complex functions it is recommended to document the parameters too.</p> ! <h3>Files containing only classes:</h3> <p>Do not forget to comment the class. Classes need a separate @package definition, it is the same as the header package name. Apart from this special case the above statement for files containing only functions needs to be applied to classes and it's methods too.</p> ! <h3>Code following the header but only functions/classes file:</h3> <p>If this case is true, the best method to avoid documentation confusions is adding an ignore command, for example:</p> ! <blockquote><pre> /** * {HEADER} --- 143,161 ---- */ {CODE} ! </pre></div> ! <h4>Files containing only functions:</h4> <p>Do not forget to comment the functions (especially the first function following the header). Each function should have at least a comment of what this function does. For more complex functions it is recommended to document the parameters too.</p> ! <h4>Files containing only classes:</h4> <p>Do not forget to comment the class. Classes need a separate @package definition, it is the same as the header package name. Apart from this special case the above statement for files containing only functions needs to be applied to classes and it's methods too.</p> ! <h4>Code following the header but only functions/classes file:</h4> <p>If this case is true, the best method to avoid documentation confusions is adding an ignore command, for example:</p> ! <div class="codebox"><pre> /** * {HEADER} *************** *** 315,327 **** */ class ... ! </pre></blockquote> ! ! </div> ! <a href="#top">Top</a> ! <br /><br /> ! <a name="locations"></a><b>1.iii. File Locations</b> ! <br /><br /> ! <div class="paragraph"> <p>Functions used by more than one page should be placed in functions.php, functions specific to one page should be placed on that page (at the bottom) or within the relevant sections functions file. Some files in <code>/includes</code> are holding functions responsible for special sections, for example uploading files, displaying "things", user related functions and so forth.</p> --- 171,177 ---- */ class ... ! </pre></div> ! <a name="locations"></a><h3>1.iii. File Locations</h3> <p>Functions used by more than one page should be placed in functions.php, functions specific to one page should be placed on that page (at the bottom) or within the relevant sections functions file. Some files in <code>/includes</code> are holding functions responsible for special sections, for example uploading files, displaying "things", user related functions and so forth.</p> *************** *** 329,337 **** <p>The following packages are defined, and related new features/functions should be placed within the mentioned files/locations, as well as specifying the correct package name. The package names are bold within this list:</p> ! <ul class="menu"> ! <li><b>phpBB3</b><br />Core files and all files not assigned to a separate package</li> ! <li><b>acm</b><br /><code>/includes/acm</code>, <code>/includes/cache.php</code><br />Cache System</li> ! <li><b>acp</b><br /><code>/adm</code>, <code>/includes/acp</code>, <code>/includes/functions_admin.php</code><br />Administration Control Panel</li> ! <li><b>dbal</b><br /><code>/includes/db</code><br />Database Abstraction Layer.<br />Base class is <code>dbal</code> <ul> <li><code>/includes/db/dbal.php</code><br />Base DBAL class, defining the overall framework as well as common detominators</li> --- 179,187 ---- <p>The following packages are defined, and related new features/functions should be placed within the mentioned files/locations, as well as specifying the correct package name. The package names are bold within this list:</p> ! <ul> ! <li><strong>phpBB3</strong><br />Core files and all files not assigned to a separate package</li> ! <li><strong>acm</strong><br /><code>/includes/acm</code>, <code>/includes/cache.php</code><br />Cache System</li> ! <li><strong>acp</strong><br /><code>/adm</code>, <code>/includes/acp</code>, <code>/includes/functions_admin.php</code><br />Administration Control Panel</li> ! <li><strong>dbal</strong><br /><code>/includes/db</code><br />Database Abstraction Layer.<br />Base class is <code>dbal</code> <ul> <li><code>/includes/db/dbal.php</code><br />Base DBAL class, defining the overall framework as well as common detominators</li> *************** *** 347,381 **** </ul> </li> ! <li><b>diff</b><br /><code>/includes/diff</code><br />Diff Engine</li> ! <li><b>docs</b><br /><code>/docs</code><br />phpBB Documentation</li> ! <li><b>images</b><br /><code>/images</code><br />All global images not connected to styles</li> ! <li><b>install</b><br /><code>/install</code><br />Installation System</li> ! <li><b>language</b><br /><code>/language</code><br />All language files</li> ! <li><b>login</b><br /><code>/includes/auth</code><br />Login Authentication Plugins</li> ! <li><b>VC</b><br /><code>/includes/captcha</code><br />CAPTCHA</li> ! <li><b>mcp</b><br /><code>mcp.php</code>, <code>/includes/mcp</code>, <code>report.php</code><br />Moderator Control Panel</li> ! <li><b>ucp</b><br /><code>ucp.php</code>, <code>/includes/ucp</code><br />User Control Panel</li> ! <li><b>utf</b><br /><code>/includes/utf</code><br />UTF8-related functions/classes</li> ! <li><b>search</b><br /><code>/includes/search</code>, <code>search.php</code><br />Search System</li> ! <li><b>styles</b><br /><code>/styles</code>, <code>style.php</code><br />phpBB Styles/Templates/Themes/Imagesets</li> </ul> </div> - <a href="#top">Top</a> - <br /><br /> ! <hr /> ! <a name="code"></a><h1>2. Code Layout/Guidelines</h1> <p>Please note that these Guidelines applies to all php, html, javascript and css files.</p> ! <a name="namingvars"></a><b>2.i. Variable/Function Naming</b> ! <br /><br /> ! <div class="paragraph"> <p>We will not be using any form of hungarian notation in our naming conventions. Many of us believe that hungarian naming is one of the primary code obfuscation techniques currently in use.</p> ! <h3>Variable Names:</h3> <p>Variable names should be in all lowercase, with words separated by an underscore, example:</p> --- 197,237 ---- </ul> </li> ! <li><strong>diff</strong><br /><code>/includes/diff</code><br />Diff Engine</li> ! <li><strong>docs</strong><br /><code>/docs</code><br />phpBB Documentation</li> ! <li><strong>images</strong><br /><code>/images</code><br />All global images not connected to styles</li> ! <li><strong>install</strong><br /><code>/install</code><br />Installation System</li> ! <li><strong>language</strong><br /><code>/language</code><br />All language files</li> ! <li><strong>login</strong><br /><code>/includes/auth</code><br />Login Authentication Plugins</li> ! <li><strong>VC</strong><br /><code>/includes/captcha</code><br />CAPTCHA</li> ! <li><strong>mcp</strong><br /><code>mcp.php</code>, <code>/includes/mcp</code>, <code>report.php</code><br />Moderator Control Panel</li> ! <li><strong>ucp</strong><br /><code>ucp.php</code>, <code>/includes/ucp</code><br />User Control Panel</li> ! <li><strong>utf</strong><br /><code>/includes/utf</code><br />UTF8-related functions/classes</li> ! <li><strong>search</strong><br /><code>/includes/search</code>, <code>search.php</code><br />Search System</li> ! <li><strong>styles</strong><br /><code>/styles</code>, <code>style.php</code><br />phpBB Styles/Templates/Themes/Imagesets</li> </ul> + </div> + + <div class="back2top"><a href="#wrap" class="top">Back to Top</a></div> + + <span class="corners-bottom"><span></span></span></div> </div> ! <hr /> ! <a name="code"></a><h2>2. Code Layout/Guidelines</h2> ! ! <div class="paragraph"> ! <div class="inner"><span class="corners-top"><span></span></span> ! ! <div class="content"> <p>Please note that these Guidelines applies to all php, html, javascript and css files.</p> ! <a name="namingvars"></a><h3>2.i. Variable/Function Naming</h3> <p>We will not be using any form of hungarian notation in our naming conventions. Many of us believe that hungarian naming is one of the primary code obfuscation techniques currently in use.</p> ! <h4>Variable Names:</h4> <p>Variable names should be in all lowercase, with words separated by an underscore, example:</p> *************** *** 386,393 **** <p>Names should be descriptive, but concise. We don't want huge sentences as our variable names, but typing an extra couple of characters is always better than wondering what exactly a certain variable is for. </p> ! <h3>Loop Indices:</h3> <p>The <em>only</em> situation where a one-character variable name is allowed is when it's the index for some looping construct. In this case, the index of the outer loop should always be $i. If there's a loop inside that loop, its index should be $j, followed by $k, and so on. If the loop is being indexed by some already-existing variable with a meaningful name, this guideline does not apply, example:</p> ! <blockquote><pre> for ($i = 0; $i < $outer_size; $i++) { --- 242,249 ---- <p>Names should be descriptive, but concise. We don't want huge sentences as our variable names, but typing an extra couple of characters is always better than wondering what exactly a certain variable is for. </p> ! <h4>Loop Indices:</h4> <p>The <em>only</em> situation where a one-character variable name is allowed is when it's the index for some looping construct. In this case, the index of the outer loop should always be $i. If there's a loop inside that loop, its index should be $j, followed by $k, and so on. If the loop is being indexed by some already-existing variable with a meaningful name, this guideline does not apply, example:</p> ! <div class="codebox"><pre> for ($i = 0; $i < $outer_size; $i++) { *************** *** 397,429 **** } } ! </pre></blockquote> ! <h3>Function Names:</h3> <p>Functions should also be named descriptively. We're not programming in C here, we don't want to write functions called things like "stristr()". Again, all lower-case names with words separated by a single underscore character. Function names should preferably have a verb in them somewhere. Good function names are <code>print_login_status()</code>, <code>get_user_data()</code>, etc. </p> ! <h3>Function Arguments:</h3> <p>Arguments are subject to the same guidelines as variable names. We don't want a bunch of functions like: <code>do_stuff($a, $b, $c)</code>. In most cases, we'd like to be able to tell how to use a function by just looking at its declaration. </p> ! <h3>Summary:</h3> <p>The basic philosophy here is to not hurt code clarity for the sake of laziness. This has to be balanced by a little bit of common sense, though; <code>print_login_status_for_a_given_user()</code> goes too far, for example -- that function would be better named <code>print_user_login_status()</code>, or just <code>print_login_status()</code>.</p> ! <h3>Special Namings: </h3> <p>For all emoticons use the term <code>smiley</code> in singular and <code>smilies</code> in plural.</p> ! </div> ! <a href="#top">Top</a> ! <br /><br /> ! ! ! <a name="codelayout"></a><b>2.ii. Code Layout</b> ! <br /><br /> ! <div class="paragraph"> ! <h3>Always include the braces:</h3> <p>This is another case of being too lazy to type 2 extra characters causing problems with code clarity. Even if the body of some construct is only one line long, do <em>not</em> drop the braces. Just don't, examples:</p> <p class="bad">// These are all wrong. </p> ! <blockquote><pre> if (condition) do_stuff(); --- 253,278 ---- } } ! </pre></div> ! <h4>Function Names:</h4> <p>Functions should also be named descriptively. We're not programming in C here, we don't want to write functions called things like "stristr()". Again, all lower-case names with words separated by a single underscore character. Function names should preferably have a verb in them somewhere. Good function names are <code>print_login_status()</code>, <code>get_user_data()</code>, etc. </p> ! <h4>Function Arguments:</h4> <p>Arguments are subject to the same guidelines as variable names. We don't want a bunch of functions like: <code>do_stuff($a, $b, $c)</code>. In most cases, we'd like to be able to tell how to use a function by just looking at its declaration. </p> ! <h4>Summary:</h4> <p>The basic philosophy here is to not hurt code clarity for the sake of laziness. This has to be balanced by a little bit of common sense, though; <code>print_login_status_for_a_given_user()</code> goes too far, for example -- that function would be better named <code>print_user_login_status()</code>, or just <code>print_login_status()</code>.</p> ! <h4>Special Namings: </h4> <p>For all emoticons use the term <code>smiley</code> in singular and <code>smilies</code> in plural.</p> ! <a name="codelayout"></a><h3>2.ii. Code Layout</h3> ! <h4>Always include the braces:</h4> <p>This is another case of being too lazy to type 2 extra characters causing problems with code clarity. Even if the body of some construct is only one line long, do <em>not</em> drop the braces. Just don't, examples:</p> <p class="bad">// These are all wrong. </p> ! <div class="codebox"><pre> if (condition) do_stuff(); *************** *** 436,443 **** for ($i = 0; $i < size; $i++) do_stuff($i); ! </pre></blockquote> <p class="good">// These are all right. </p> ! <blockquote><pre> if (condition) { --- 285,292 ---- for ($i = 0; $i < size; $i++) do_stuff($i); ! </pre></div> <p class="good">// These are all right. </p> ! <div class="codebox"><pre> if (condition) { *************** *** 454,463 **** do_stuff(); } ! </pre></blockquote> ! <h3>Where to put the braces:</h3> <p>This one is a bit of a holy war, but we're going to use a style that can be summed up in one sentence: Braces always go on their own line. The closing brace should also always be at the same column as the corresponding opening brace, examples:</p> ! <blockquote><pre> if (condition) { --- 303,312 ---- do_stuff(); } ! </pre></div> ! <h4>Where to put the braces:</h4> <p>This one is a bit of a holy war, but we're going to use a style that can be summed up in one sentence: Braces always go on their own line. The closing brace should also always be at the same column as the corresponding opening brace, examples:</p> ! <div class="codebox"><pre> if (condition) { *************** *** 476,497 **** ... } ! while (condition) { ... } ! function do_stuff() { ... } ! </pre></blockquote> ! <h3>Use spaces between tokens:</h3> <p>This is another simple, easy step that helps keep code readable without much effort. Whenever you write an assignment, expression, etc.. Always leave <em>one</em> space between the tokens. Basically, write code as if it was English. Put spaces between variable names and operators. Don't put spaces just after an opening bracket or before a closing bracket. Don't put spaces just before a comma or a semicolon. This is best shown with a few examples, examples:</p> <p>// Each pair shows the wrong way followed by the right way. </p> ! <blockquote><pre> $i=0; $i = 0; --- 325,346 ---- ... } ! while (condition) { ... } ! function do_stuff() { ... } ! </pre></div> ! <h4>Use spaces between tokens:</h4> <p>This is another simple, easy step that helps keep code readable without much effort. Whenever you write an assignment, expression, etc.. Always leave <em>one</em> space between the tokens. Basically, write code as if it was English. Put spaces between variable names and operators. Don't put spaces just after an opening bracket or before a closing bracket. Don't put spaces just before a comma or a semicolon. This is best shown with a few examples, examples:</p> <p>// Each pair shows the wrong way followed by the right way. </p> ! <div class="codebox"><pre> $i=0; $i = 0; *************** *** 511,604 **** $i=($j < $size)?0:1; $i = ($j < $size) ? 0 : 1; ! </pre></blockquote> ! <h3>Operator precedence:</h3> <p>Do you know the exact precedence of all the operators in PHP? Neither do I. Don't guess. Always make it obvious by using brackets to force the precedence of an equation so you know what it does. Remember to not over-use this, as it may harden the readability. Basically, do not enclose single expressions. Examples:</p> <p class="bad">// what's the result? who knows. </p> ! <blockquote><pre> $bool = ($i < 7 && $j > 8 || $k == 4); ! </pre></blockquote> <p class="bad">// now you can be certain what I'm doing here.</p> ! <blockquote><pre> $bool = (($i < 7) && (($j < 8) || ($k == 4))); ! </pre></blockquote> <p class="good">// But this one is even better, because it is easier on the eye but the intention is preserved</p> ! <blockquote><pre> $bool = ($i < 7 && ($j < 8 || $k == 4)); ! </pre></blockquote> ! <h3>Quoting strings:</h3> <p>There are two different ways to quote strings in PHP - either with single quotes or with double quotes. The main difference is that the parser does variable interpolation in double-quoted strings, but not in single quoted strings. Because of this, you should <em>always</em> use single quotes <em>unless</em> you specifically need variable interpolation to be done on that string. This way, we can save the parser the trouble of parsing a bunch of strings where no interpolation needs to be done.</p> <p>Also, if you are using a string variable as part of a function call, you do not need to enclose that variable in quotes. Again, this will just make unnecessary work for the parser. Note, however, that nearly all of the escape sequences that exist for double-quoted strings will not work with single-quoted strings. Be careful, and feel free to break this guideline if it's making your code easier to read, examples:</p> <p class="bad">// wrong </p> ! <blockquote><pre> $str = "This is a really long string with no variables for the parser to find."; do_stuff("$str"); ! </pre></blockquote> <p class="good">// right</p> ! <blockquote><pre> $str = 'This is a really long string with no variables for the parser to find.'; do_stuff($str); ! </pre></blockquote> <p class="bad">// Sometimes single quotes are just not right</p> ! <blockquote><pre> $post_url = $phpbb_root_path . 'posting.' . $phpEx . '?mode=' . $mode . '&amp;start=' . $start; ! </pre></blockquote> <p class="good">// Double quotes are sometimes needed to not overcroud the line with concentinations</p> ! <blockquote><pre> $post_url = "{$phpbb_root_path}posting.$phpEx?mode=$mode&amp;start=$start"; ! </pre></blockquote> <p>In SQL Statements mixing single and double quotes is partly allowed (following the guidelines listed here about SQL Formatting), else it should be tryed to only use one method - mostly single quotes.</p> ! <h3>Associative array keys:</h3> <p>In PHP, it's legal to use a literal string as a key to an associative array without quoting that string. We don't want to do this -- the string should always be quoted to avoid confusion. Note that this is only when we're using a literal, not when we're using a variable, examples:</p> <p class="bad">// wrong</p> ! <blockquote><pre> $foo = $assoc_array[blah]; ! </pre></blockquote> <p class="good">// right </p> ! <blockquote><pre> $foo = $assoc_array['blah']; ! </pre></blockquote> <p class="bad">// wrong</p> ! <blockquote><pre> $foo = $assoc_array["$var"]; ! </pre></blockquote> <p class="good">// right </p> ! <blockquote><pre> $foo = $assoc_array[$var]; ! </pre></blockquote> ! <h3>Comments:</h3> <p>Each complex function should be preceded by a comment that tells a programmer everything they need to know to use that function. The meaning of every parameter, the expected input, and the output are required as a minimal comment. The function's behaviour in error conditions (and what those error conditions are) should also be present - but mostly included within the comment about the output.<br /><br />Especially important to document are any assumptions the code makes, or preconditions for its proper operation. Any one of the developers should be able to look at any part of the application and figure out what's going on in a reasonable amount of time.<br /><br />Avoid using <code>/* */</code> comment blocks for one-line comments, <code>//</code> should be used for one/two-liners.</p> ! <h3>Magic numbers:</h3> <p>Don't use them. Use named constants for any literal value other than obvious special cases. Basically, it's ok to check if an array has 0 elements by using the literal 0. It's not ok to assign some special meaning to a number and then use it everywhere as a literal. This hurts readability AND maintainability. The constants <code>true</code> and <code>false</code> should be used in place of the literals 1 and 0 -- even though they have the same values (but not type!), it's more obvious what the actual logic is when you use the named constants. Typecast variables where it is needed, do not rely on the correct variable type (PHP is currently very loose on typecasting which can lead to security problems if a developer does not have a very close eye to it).</p> ! <h3>Shortcut operators:</h3> <p>The only shortcut operators that cause readability problems are the shortcut increment <code>$i++</code> and decrement <code>$j--</code> operators. These operators should not be used as part of an expression. They can, however, be used on their own line. Using them in expressions is just not worth the headaches when debugging, examples:</p> <p class="bad">// wrong </p> ! <blockquote><pre> $array[++$i] = $j; $array[$i++] = $k; ! </pre></blockquote> <p class="good">// right </p> ! <blockquote><pre> $i++; $array[$i] = $j; --- 360,453 ---- $i=($j < $size)?0:1; $i = ($j < $size) ? 0 : 1; ! </pre></div> ! <h4>Operator precedence:</h4> <p>Do you know the exact precedence of all the operators in PHP? Neither do I. Don't guess. Always make it obvious by using brackets to force the precedence of an equation so you know what it does. Remember to not over-use this, as it may harden the readability. Basically, do not enclose single expressions. Examples:</p> <p class="bad">// what's the result? who knows. </p> ! <div class="codebox"><pre> $bool = ($i < 7 && $j > 8 || $k == 4); ! </pre></div> <p class="bad">// now you can be certain what I'm doing here.</p> ! <div class="codebox"><pre> $bool = (($i < 7) && (($j < 8) || ($k == 4))); ! </pre></div> <p class="good">// But this one is even better, because it is easier on the eye but the intention is preserved</p> ! <div class="codebox"><pre> $bool = ($i < 7 && ($j < 8 || $k == 4)); ! </pre></div> ! <h4>Quoting strings:</h4> <p>There are two different ways to quote strings in PHP - either with single quotes or with double quotes. The main difference is that the parser does variable interpolation in double-quoted strings, but not in single quoted strings. Because of this, you should <em>always</em> use single quotes <em>unless</em> you specifically need variable interpolation to be done on that string. This way, we can save the parser the trouble of parsing a bunch of strings where no interpolation needs to be done.</p> <p>Also, if you are using a string variable as part of a function call, you do not need to enclose that variable in quotes. Again, this will just make unnecessary work for the parser. Note, however, that nearly all of the escape sequences that exist for double-quoted strings will not work with single-quoted strings. Be careful, and feel free to break this guideline if it's making your code easier to read, examples:</p> <p class="bad">// wrong </p> ! <div class="codebox"><pre> $str = "This is a really long string with no variables for the parser to find."; do_stuff("$str"); ! </pre></div> <p class="good">// right</p> ! <div class="codebox"><pre> $str = 'This is a really long string with no variables for the parser to find.'; do_stuff($str); ! </pre></div> <p class="bad">// Sometimes single quotes are just not right</p> ! <div class="codebox"><pre> $post_url = $phpbb_root_path . 'posting.' . $phpEx . '?mode=' . $mode . '&amp;start=' . $start; ! </pre></div> <p class="good">// Double quotes are sometimes needed to not overcroud the line with concentinations</p> ! <div class="codebox"><pre> $post_url = "{$phpbb_root_path}posting.$phpEx?mode=$mode&amp;start=$start"; ! </pre></div> <p>In SQL Statements mixing single and double quotes is partly allowed (following the guidelines listed here about SQL Formatting), else it should be tryed to only use one method - mostly single quotes.</p> ! <h4>Associative array keys:</h4> <p>In PHP, it's legal to use a literal string as a key to an associative array without quoting that string. We don't want to do this -- the string should always be quoted to avoid confusion. Note that this is only when we're using a literal, not when we're using a variable, examples:</p> <p class="bad">// wrong</p> ! <div class="codebox"><pre> $foo = $assoc_array[blah]; ! </pre></div> <p class="good">// right </p> ! <div class="codebox"><pre> $foo = $assoc_array['blah']; ! </pre></div> <p class="bad">// wrong</p> ! <div class="codebox"><pre> $foo = $assoc_array["$var"]; ! </pre></div> <p class="good">// right </p> ! <div class="codebox"><pre> $foo = $assoc_array[$var]; ! </pre></div> ! <h4>Comments:</h4> <p>Each complex function should be preceded by a comment that tells a programmer everything they need to know to use that function. The meaning of every parameter, the expected input, and the output are required as a minimal comment. The function's behaviour in error conditions (and what those error conditions are) should also be present - but mostly included within the comment about the output.<br /><br />Especially important to document are any assumptions the code makes, or preconditions for its proper operation. Any one of the developers should be able to look at any part of the application and figure out what's going on in a reasonable amount of time.<br /><br />Avoid using <code>/* */</code> comment blocks for one-line comments, <code>//</code> should be used for one/two-liners.</p> ! <h4>Magic numbers:</h4> <p>Don't use them. Use named constants for any literal value other than obvious special cases. Basically, it's ok to check if an array has 0 elements by using the literal 0. It's not ok to assign some special meaning to a number and then use it everywhere as a literal. This hurts readability AND maintainability. The constants <code>true</code> and <code>false</code> should be used in place of the literals 1 and 0 -- even though they have the same values (but not type!), it's more obvious what the actual logic is when you use the named constants. Typecast variables where it is needed, do not rely on the correct variable type (PHP is currently very loose on typecasting which can lead to security problems if a developer does not have a very close eye to it).</p> ! <h4>Shortcut operators:</h4> <p>The only shortcut operators that cause readability problems are the shortcut increment <code>$i++</code> and decrement <code>$j--</code> operators. These operators should not be used as part of an expression. They can, however, be used on their own line. Using them in expressions is just not worth the headaches when debugging, examples:</p> <p class="bad">// wrong </p> ! <div class="codebox"><pre> $array[++$i] = $j; $array[$i++] = $k; ! </pre></div> <p class="good">// right </p> ! <div class="codebox"><pre> $i++; $array[$i] = $j; *************** *** 606,649 **** $array[$i] = $k; $i++; ! </pre></blockquote> ! <h3>Inline conditionals:</h3> <p>Inline conditionals should only be used to do very simple things. Preferably, they will only be used to do assignments, and not for function calls or anything complex at all. They can be harmful to readability if used incorrectly, so don't fall in love with saving typing by using them, examples:</p> <p class="bad">// Bad place to use them</p> ! <blockquote><pre> ($i < $size && $j > $size) ? do_stuff($foo) : do_stuff($bar); ! </pre></blockquote> <p class="good">// OK place to use them </p> ! <blockquote><pre> $min = ($i < $j) ? $i : $j; ! </pre></blockquote> ! <h3>Don't use uninitialized variables.</h3> <p>For phpBB3, we intend to use a higher level of run-time error reporting. This will mean that the use of an uninitialized variable will be reported as a warning. These warnings can be avoided by using the built-in isset() function to check whether a variable has been set - but preferably the variable is always existing. For checking if an array has a key set this can come in handy though, examples:</p> <p class="bad">// Wrong </p> ! <blockquote><pre> if ($forum) ... ! </pre></blockquote> <p class="good">// Right </p> ! <blockquote><pre> if (isset($forum)) ... ! </pre></blockquote> <p class="good">// Also possible</p> ! <blockquote><pre> if (isset($forum) && $forum == 5) ! </pre></blockquote> <p>The <code>empty()</code> function is useful if you want to check if a variable is not set or being empty (an empty string, 0 as an integer or string, NULL, false, an empty array or a variable declared, but without a value in a class). Therefore empty should be used in favor of <code>isset($array) && sizeof($array) > 0</code> - this can be written in a shorter way as <code>!empty($array)</code>.</p> ! <h3>Switch statements:</h3> <p>Switch/case code blocks can get a bit long sometimes. To have some level of notice and being in-line with the opening/closing brace requirement (where they are on the same line for better readability), this also applies to switch/case code blocks and the breaks. An example:</p> <p class="bad">// Wrong </p> ! <blockquote><pre> switch ($mode) { --- 455,498 ---- $array[$i] = $k; $i++; ! </pre></div> ! <h4>Inline conditionals:</h4> <p>Inline conditionals should only be used to do very simple things. Preferably, they will only be used to do assignments, and not for function calls or anything complex at all. They can be harmful to readability if used incorrectly, so don't fall in love with saving typing by using them, examples:</p> <p class="bad">// Bad place to use them</p> ! <div class="codebox"><pre> ($i < $size && $j > $size) ? do_stuff($foo) : do_stuff($bar); ! </pre></div> <p class="good">// OK place to use them </p> ! <div class="codebox"><pre> $min = ($i < $j) ? $i : $j; ! </pre></div> ! <h4>Don't use uninitialized variables.</h4> <p>For phpBB3, we intend to use a higher level of run-time error reporting. This will mean that the use of an uninitialized variable will be reported as a warning. These warnings can be avoided by using the built-in isset() function to check whether a variable has been set - but preferably the variable is always existing. For checking if an array has a key set this can come in handy though, examples:</p> <p class="bad">// Wrong </p> ! <div class="codebox"><pre> if ($forum) ... ! </pre></div> <p class="good">// Right </p> ! <div class="codebox"><pre> if (isset($forum)) ... ! </pre></div> <p class="good">// Also possible</p> ! <div class="codebox"><pre> if (isset($forum) && $forum == 5) ! </pre></div> <p>The <code>empty()</code> function is useful if you want to check if a variable is not set or being empty (an empty string, 0 as an integer or string, NULL, false, an empty array or a variable declared, but without a value in a class). Therefore empty should be used in favor of <code>isset($array) && sizeof($array) > 0</code> - this can be written in a shorter way as <code>!empty($array)</code>.</p> ! <h4>Switch statements:</h4> <p>Switch/case code blocks can get a bit long sometimes. To have some level of notice and being in-line with the opening/closing brace requirement (where they are on the same line for better readability), this also applies to switch/case code blocks and the breaks. An example:</p> <p class="bad">// Wrong </p> ! <div class="codebox"><pre> switch ($mode) { *************** *** 655,662 **** break; } ! </pre></blockquote> <p class="good">// Good </p> ! <blockquote><pre> switch ($mode) { --- 504,511 ---- break; } ! </pre></div> <p class="good">// Good </p> ! <div class="codebox"><pre> switch ($mode) { *************** *** 673,680 **** break; } ! </pre></blockquote> <p class="good">// Also good, if you have more code between the case and the break </p> ! <blockquote><pre> switch ($mode) { --- 522,529 ---- break; } ! </pre></div> <p class="good">// Also good, if you have more code between the case and the break </p> ! <div class="codebox"><pre> switch ($mode) { *************** *** 697,701 **** break; } ! </pre></blockquote> <p>Even if the break for the default case is not needed, it is sometimes better to include it just for readability and completeness.</p> --- 546,550 ---- break; } ! </pre></div> <p>Even if the break for the default case is not needed, it is sometimes better to include it just for readability and completeness.</p> *************** *** 704,708 **** <p class="good">// Example with no break </p> ! <blockquote><pre> switch ($mode) { --- 553,557 ---- <p class="good">// Example with no break </p> ! <div class="codebox"><pre> switch ($mode) { *************** *** 725,748 **** break; } ! </pre></blockquote> ! ! ! </div> ! <a href="#top">Top</a> ! <br /><br /> ! ! <a name="sql"></a><b>2.iii. SQL/SQL Layout</b> ! <br /><br /> ! <div class="paragraph"> ! <h3>Common SQL Guidelines: </h3> <p>All SQL should be cross-DB compatible, if DB specific SQL is used alternatives must be provided which work on all supported DB's (MySQL3/4/5, MSSQL (7.0 and 2000), PostgreSQL (7.0+), Firebird, SQLite, Oracle8, ODBC (generalised if possible)).</p> <p>All SQL commands should utilise the DataBase Abstraction Layer (DBAL)</p> ! <h3>SQL code layout:</h3> <p>SQL Statements are often unreadable without some formatting, since they tend to be big at times. Though the formatting of sql statements adds a lot to the readability of code. SQL statements should be formatted in the following way, basically writing keywords: </p> ! <blockquote><pre> $sql = 'SELECT * <-one tab->FROM ' . SOME_TABLE . ' --- 574,589 ---- break; } ! </pre></div> ! <a name="sql"></a><h3>2.iii. SQL/SQL Layout</h3> ! <h4>Common SQL Guidelines: </h4> <p>All SQL should be cross-DB compatible, if DB specific SQL is used alternatives must be provided which work on all supported DB's (MySQL3/4/5, MSSQL (7.0 and 2000), PostgreSQL (7.0+), Firebird, SQLite, Oracle8, ODBC (generalised if possible)).</p> <p>All SQL commands should utilise the DataBase Abstraction Layer (DBAL)</p> ! <h4>SQL code layout:</h4> <p>SQL Statements are often unreadable without some formatting, since they tend to be big at times. Though the formatting of sql statements adds a lot to the readability of code. SQL statements should be formatted in the following way, basically writing keywords: </p> ! <div class="codebox"><pre> $sql = 'SELECT * <-one tab->FROM ' . SOME_TABLE . ' *************** *** 751,759 **** <-three tabs->OR b = 3) <-one tab->ORDER BY b'; ! </pre></blockquote> <p>Here the example with the tabs applied:</p> ! <blockquote><pre> $sql = 'SELECT * FROM ' . SOME_TABLE . ' --- 592,600 ---- <-three tabs->OR b = 3) <-one tab->ORDER BY b'; ! </pre></div> <p>Here the example with the tabs applied:</p> ! <div class="codebox"><pre> $sql = 'SELECT * FROM ' . SOME_TABLE . ' *************** *** 762,817 **** OR b = 3) ORDER BY b'; ! </pre></blockquote> ! <h3>SQL Quotes: </h3> <p>Double quotes where applicable (The variables in these examples are typecasted to integers before) ... examples: </p> <p class="bad">// These are wrong.</p> ! <blockquote><pre> "UPDATE " . SOME_TABLE . " SET something = something_else WHERE a = $b"; 'UPDATE ' . SOME_TABLE . ' SET something = ' . $user_id . ' WHERE a = ' . $something; ! </pre></blockquote> <p class="good">// These are right. </p> ! <blockquote><pre> 'UPDATE ' . SOME_TABLE . " SET something = something_else WHERE a = $b"; 'UPDATE ' . SOME_TABLE . " SET something = $user_id WHERE a = $something"; ! </pre></blockquote> <p>In other words use single quotes where no variable substitution is required or where the variable involved shouldn't appear within double quotes. Otherwise use double quotes.</p> ! <h3>Avoid DB specific SQL: </h3> <p>The "not equals operator", as defined by the SQL:2003 standard, is "<>"</p> <p class="bad">// This is wrong.</p> ! <blockquote><pre> $sql = 'SELECT * FROM ' . SOME_TABLE . ' WHERE a != 2'; ! </pre></blockquote> <p class="good">// This is right. </p> ! <blockquote><pre> $sql = 'SELECT * FROM ' . SOME_TABLE . ' ! WHERE a <> 2'; ! </pre></blockquote> ! <h3>Common DBAL methods: </h3> ! <h3>sql_escape():</h3> <p>Always use <code>$db->sql_escape()</code> if you need to check for a string within an SQL statement (even if you are sure the variable cannot contain single quotes - never trust your input), for example:</p> ! <blockquote><pre> $sql = 'SELECT * FROM ' . SOME_TABLE . " WHERE username = '" . $db->sql_escape($username) . "'"; ! </pre></blockquote> ! <h3>sql_query_limit():</h3> <p>We do not add limit statements to the sql query, but instead use <code>$db->sql_query_limit()</code>. You basically pass the query, the total number of lines to retrieve and the offset.</p> --- 603,658 ---- OR b = 3) ORDER BY b'; ! </pre></div> ! <h4>SQL Quotes: </h4> <p>Double quotes where applicable (The variables in these examples are typecasted to integers before) ... examples: </p> <p class="bad">// These are wrong.</p> ! <div class="codebox"><pre> "UPDATE " . SOME_TABLE . " SET something = something_else WHERE a = $b"; 'UPDATE ' . SOME_TABLE . ' SET something = ' . $user_id . ' WHERE a = ' . $something; ! </pre></div> <p class="good">// These are right. </p> ! <div class="codebox"><pre> 'UPDATE ' . SOME_TABLE . " SET something = something_else WHERE a = $b"; 'UPDATE ' . SOME_TABLE . " SET something = $user_id WHERE a = $something"; ! </pre></div> <p>In other words use single quotes where no variable substitution is required or where the variable involved shouldn't appear within double quotes. Otherwise use double quotes.</p> ! <h4>Avoid DB specific SQL: </h4> <p>The "not equals operator", as defined by the SQL:2003 standard, is "<>"</p> <p class="bad">// This is wrong.</p> ! <div class="codebox"><pre> $sql = 'SELECT * FROM ' . SOME_TABLE . ' WHERE a != 2'; ! </pre></div> <p class="good">// This is right. </p> ! <div class="codebox"><pre> $sql = 'SELECT * FROM ' . SOME_TABLE . ' ! WHERE a <> 2'; ! </pre></div> ! <h4>Common DBAL methods: </h4> ! <h4>sql_escape():</h4> <p>Always use <code>$db->sql_escape()</code> if you need to check for a string within an SQL statement (even if you are sure the variable cannot contain single quotes - never trust your input), for example:</p> ! <div class="codebox"><pre> $sql = 'SELECT * FROM ' . SOME_TABLE . " WHERE username = '" . $db->sql_escape($username) . "'"; ! </pre></div> ! <h4>sql_query_limit():</h4> <p>We do not add limit statements to the sql query, but instead use <code>$db->sql_query_limit()</code>. You basically pass the query, the total number of lines to retrieve and the offset.</p> *************** *** 821,829 **** <p>Make sure when using something like "SELECT x.*, y.jars" that there is not a column named jars in x; make sure that there is no overlap between an implicit column and the explicit columns.</p> ! <h3>sql_build_array():</h3> <p>If you need to UPDATE or INSERT data, make use of the <code>$db->sql_build_array()</code> function. This function already escapes strings and checks other types, so there is no need to do this here. The data to be inserted should go into an array - <code>$sql_ary</code> - or directly within the statement if one or two variables needs to be inserted/updated. An example of an insert statement would be:</p> ! <blockquote><pre> $sql_ary = array( 'somedata' => $my_string, --- 662,670 ---- <p>Make sure when using something like "SELECT x.*, y.jars" that there is not a column named jars in x; make sure that there is no overlap between an implicit column and the explicit columns.</p> ! <h4>sql_build_array():</h4> <p>If you need to UPDATE or INSERT data, make use of the <code>$db->sql_build_array()</code> function. This function already escapes strings and checks other types, so there is no need to do this here. The data to be inserted should go into an array - <code>$sql_ary</code> - or directly within the statement if one or two variables needs to be inserted/updated. An example of an insert statement would be:</p> ! <div class="codebox"><pre> $sql_ary = array( 'somedata' => $my_string, *************** *** 833,841 **** $db->sql_query('INSERT INTO ' . SOME_TABLE . ' ' . $db->sql_build_array('INSERT', $sql_ary)); ! </pre></blockquote> <p>To complete the example, this is how an update statement would look like:</p> ! <blockquote><pre> $sql_ary = array( 'somedata' => $my_string, --- 674,682 ---- $db->sql_query('INSERT INTO ' . SOME_TABLE . ' ' . $db->sql_build_array('INSERT', $sql_ary)); ! </pre></div> <p>To complete the example, this is how an update statement would look like:</p> ! <div class="codebox"><pre> $sql_ary = array( 'somedata' => $my_string, *************** *** 848,865 **** WHERE user_id = ' . (int) $user_id; $db->sql_query($sql); ! </pre></blockquote> <p>The <code>$db->sql_build_array()</code> function supports the following modes: <code>INSERT</code> (example above), <code>INSERT_SELECT</code> (building query for <code>INSERT INTO table (...) SELECT value, column ...</code> statements), <code>MULTI_INSERT</code> (for returning extended inserts), <code>UPDATE</code> (example above) and <code>SELECT</code> (for building WHERE statement [AND logic]).</p> ! <h3>sql_in_set():</h3> <p>The <code>$db->sql_in_set()</code> function should be used for building <code>IN ()</code> and <code>NOT IN ()</code> constructs. Since (specifically) MySQL tend to be faster if for one value to be compared the <code>=</code> and <code><></code> operator is used, we let the DBAL decide what to do. A typical example of doing a positive match against a number of values would be:</p> ! <blockquote><pre> $sql = 'SELECT * FROM ' . FORUMS_TABLE . ' WHERE ' . $db->sql_in_set('forum_id', $forum_ids); $db->sql_query($sql); ! </pre></blockquote> <p>Based on the number of values in $forum_ids, the query can look differently.</p> --- 689,706 ---- WHERE user_id = ' . (int) $user_id; $db->sql_query($sql); ! </pre></div> <p>The <code>$db->sql_build_array()</code> function supports the following modes: <code>INSERT</code> (example above), <code>INSERT_SELECT</code> (building query for <code>INSERT INTO table (...) SELECT value, column ...</code> statements), <code>MULTI_INSERT</code> (for returning extended inserts), <code>UPDATE</code> (example above) and <code>SELECT</code> (for building WHERE statement [AND logic]).</p> ! <h4>sql_in_set():</h4> <p>The <code>$db->sql_in_set()</code> function should be used for building <code>IN ()</code> and <code>NOT IN ()</code> constructs. Since (specifically) MySQL tend to be faster if for one value to be compared the <code>=</code> and <code><></code> operator is used, we let the DBAL decide what to do. A typical example of doing a positive match against a number of values would be:</p> ! <div class="codebox"><pre> $sql = 'SELECT * FROM ' . FORUMS_TABLE . ' WHERE ' . $db->sql_in_set('forum_id', $forum_ids); $db->sql_query($sql); ! </pre></div> <p>Based on the number of values in $forum_ids, the query can look differently.</p> *************** *** 867,888 **** <p class="good">// SQL Statement if $forum_ids = array(1, 2, 3);</p> ! <blockquote><pre> SELECT FROM phpbb_forums WHERE forum_id IN (1, 2, 3) ! </pre></blockquote> <p class="good">// SQL Statement if $forum_ids = array(1) or $forum_ids = 1</p> ! <blockquote><pre> SELECT FROM phpbb_forums WHERE forum_id = 1 ! </pre></blockquote> <p>Of course the same is possible for doing a negative match against a number of values:</p> ! <blockquote><pre> $sql = 'SELECT * FROM ' . FORUMS_TABLE . ' WHERE ' . $db->sql_in_set('forum_id', $forum_ids, <strong>true</strong>); $db->sql_query($sql); ! </pre></blockquote> <p>Based on the number of values in $forum_ids, the query can look differently here too.</p> --- 708,729 ---- <p class="good">// SQL Statement if $forum_ids = array(1, 2, 3);</p> ! <div class="codebox"><pre> SELECT FROM phpbb_forums WHERE forum_id IN (1, 2, 3) ! </pre></div> <p class="good">// SQL Statement if $forum_ids = array(1) or $forum_ids = 1</p> ! <div class="codebox"><pre> SELECT FROM phpbb_forums WHERE forum_id = 1 ! </pre></div> <p>Of course the same is possible for doing a negative match against a number of values:</p> ! <div class="codebox"><pre> $sql = 'SELECT * FROM ' . FORUMS_TABLE . ' WHERE ' . $db->sql_in_set('forum_id', $forum_ids, <strong>true</strong>); $db->sql_query($sql); ! </pre></div> <p>Based on the number of values in $forum_ids, the query can look differently here too.</p> *************** *** 890,910 **** <p class="good">// SQL Statement if $forum_ids = array(1, 2, 3);</p> ! <blockquote><pre> SELECT FROM phpbb_forums WHERE forum_id <strong>NOT</strong> IN (1, 2, 3) ! </pre></blockquote> <p class="good">// SQL Statement if $forum_ids = array(1) or $forum_ids = 1</p> ! <blockquote><pre> SELECT FROM phpbb_forums WHERE forum_id <strong><></strong> 1 ! </pre></blockquote> <p>If the given array is empty, an error will be produced.</p> ! <h3>sql_build_query():</h3> <p>The <code>$db->sql_build_query()</code> function is responsible for building sql statements f... [truncated message content] |