Scripting Guidelines
From MemHT Wiki
Contents |
Comments
Every function, class or "strange" and complicated script should be commented, indicating what does it do, helping other developers to understand how it works.
At the moment the comment style is not important, both multiline /* */ or single line // types can be used.
Indentation
Indenting your code is very important to maintain a nice and clear structure (long and not indented code is very hard to understand).
| Wrong |
class myClass() { var $foo = "Foo"; function myFunction() { echo $this->foo; } } function foo() { global $dblink; if (condition) { echo "True"; } else { echo "False"; } } |
| Correct |
class myClass() { var $foo = "Foo"; function myFunction() { echo $this->foo; } } function foo() { global $dblink; if (condition) { echo "True"; } else { echo "False"; } } |
Alignment
Alignment of your code is also very important
$myArray = array( 'One' => '1', 'Two' => '2', 'Three' => '3', 'Four' => '4', 'Five' => '5' ) $var = myFunction($var); $variable = myFunction($var); $another = myFunction($var);
If statements
Do not use long if statements
if (cond) { //... } else if (cond2) { //... } else if (cond3) { //... } else { //... }
Use switch instead
switch ($var) { case 1: //... break; case 2: //... break; case 3: //... break; default: //Same as else //... break; }
HTML tags
Do not use XHTML form (slash) in closing tags
| Wrong |
<input ..... /><br /> |
Remember that the DOCTYPE on MemHT is HTML 4.01 Transitional
| Correct |
<input .....><br> |
Variables, Classes and Function names
Use lowercase names, joining different words without spaces and with the first letter capitalized
$variable = "Foo"; $myVariable; myFunction(); capitalizeFirstLetterOfNewWords();
Constant variables names should be uppercase and have an underscore _ as their first and last character
_FOO_ = "Constant"; _MYCONSTANT_;
Security
Filter always all ingoing and outgoing data from the database using the inCode and outCode functions. You cannot trust to anyone!
Outgoing data
$row = $dblink->get_row("SELECT value FROM table WHERE id=$id"); $name = outCode($row['value']);
Ingoing data
$var = inCode($_GET['value']);
Database communication
Do not use mysql_query, mysql_fetch_assoc and other dedicated functions to communicate with the database!
From the 2.5 version, using the bundled database class is the best solution to assure the database compatibility of your scripts in the future.
If you need to communicate with the database from external scripts use the following code to connect to the database:
require_once("inc/inc_config.php"); require_once("inc/inc_database.php"); $dblink = new database(); $dblink->connect(); // YOUR CODE GOES HERE $dblink->disconnect();
Syntax:
//---------------------------------------------------------------- // GENERIC QUERY (Insert, Update, Delete...) //---------------------------------------------------------------- //Original MySQL mysql_query("UPDATE table SET value='$value' WHERE id='$id'"); //MemHT Portal class $dblink->query("UPDATE table SET value='$value' WHERE id='$id'"); //---------------------------------------------------------------- // FETCH ROW //---------------------------------------------------------------- //Original MySQL $row = mysql_fetch_assoc(mysql_query("SELECT value FROM table WHERE id='$id'")); $name = $row['value']; //MemHT Portal class $row = $dblink->get_row("SELECT value FROM table WHERE id='$id'"); $name = $row['value']; //---------------------------------------------------------------- // FETCH LIST //---------------------------------------------------------------- //Original MySQL $result = mysql_query("SELECT * FROM table ORDER BY id"); while ($row = mysql_fetch_assoc($result)) { echo $row['value']; } //MemHT Portal class $result = $dblink->get_list("SELECT * FROM table ORDER BY id"); foreach ($result as $row) { echo $row['value']; } //---------------------------------------------------------------- // NUM ROWS //---------------------------------------------------------------- //Original MySQL $num = mysql_num_rows(mysql_query("SELECT * FROM table")); //MemHT Portal class $num = $dblink->get_num("SELECT * FROM table");
Do not forget to put global $dblink; in functions when using database connections and you want to use the existing connection
function example() { global $dblink; $row = $dblink->get_row("SELECT value FROM table WHERE id=12"); //.... }
| Languages | العربية • Bahasa Indonesia • Bosanski • Български • Dansk • Deutsch • English • Español • فارسی • Français • Galego • עברית • Italiano • Magyar • Македонски • Nederlands • Português • Русский • Српски/Srpski • Svenska • Türkçe • Українська • Tiếng Việt |
