[2] | 1 | 1) Format your code so that we can read it, please! |
---|
| 2 | |
---|
| 3 | 2) Use tabs for formatting, NOT SPACES. Tabs create smaller files and editors allow |
---|
| 4 | developers to view a tab as however many spaces as they prefer. Spaces do not allow this. |
---|
| 5 | There is one exception (see #11 below). |
---|
| 6 | |
---|
| 7 | 3) Use ' instead of " for strings. This is a performance issue, and prevents |
---|
| 8 | a lot of inconsistent coding styles. |
---|
| 9 | |
---|
| 10 | 4) Comments go on the line ABOVE the code, NOT to the right of the code! |
---|
| 11 | |
---|
| 12 | 5) For each section of code put a section divider with basic explanation of the following |
---|
| 13 | code/functions. It should look like this: |
---|
| 14 | |
---|
| 15 | /****************************************************************************\ |
---|
| 16 | * These functions are used to pick my nose * |
---|
| 17 | \****************************************************************************/ |
---|
| 18 | |
---|
| 19 | 6) Do not document every bit of code in comments. PHP is an interpreted language and it will be |
---|
| 20 | nasty on performance. |
---|
| 21 | |
---|
| 22 | 7) Use switch statements where many elseif's are going to be used. Switch is faster and I like it |
---|
| 23 | better! |
---|
| 24 | |
---|
| 25 | 8) 'If' statements need to use the following format: |
---|
| 26 | |
---|
| 27 | if ($var == 'example') |
---|
| 28 | { |
---|
| 29 | echo 'This is only an example'; |
---|
| 30 | } |
---|
| 31 | else |
---|
| 32 | { |
---|
| 33 | echo 'This is not a test. This is the real thing'; |
---|
| 34 | } |
---|
| 35 | |
---|
| 36 | Do NOT make if statements like this: |
---|
| 37 | |
---|
| 38 | if ($var == 'example'){ echo 'An example'; } |
---|
| 39 | |
---|
| 40 | All other styles are not to be used. This is it. Use it or I will personally come and nag you to |
---|
| 41 | death. |
---|
| 42 | |
---|
| 43 | 9) ALL 'if' statements MUST have matching { } (brackets). Do NOT create 'if' statements like this: |
---|
| 44 | |
---|
| 45 | if ($a == b) |
---|
| 46 | dosomething(); |
---|
| 47 | |
---|
| 48 | or: |
---|
| 49 | |
---|
| 50 | if ($a == b) dosomething(); |
---|
| 51 | |
---|
| 52 | They make the code more difficult to read and follow. |
---|
| 53 | |
---|
| 54 | 10) class/function format: |
---|
| 55 | |
---|
| 56 | class testing |
---|
| 57 | { |
---|
| 58 | function print_to_screen() |
---|
| 59 | { |
---|
| 60 | if($var == 'example') |
---|
| 61 | { |
---|
| 62 | echo 'This is only an example'; |
---|
| 63 | } |
---|
| 64 | else |
---|
| 65 | { |
---|
| 66 | echo 'This is not a test. This is the real thing'; |
---|
| 67 | } |
---|
| 68 | } |
---|
| 69 | } |
---|
| 70 | |
---|
| 71 | 11) Associative arrays must be written in the following manner: |
---|
| 72 | |
---|
| 73 | $array = array( |
---|
| 74 | 'var' => 'value', |
---|
| 75 | 'var2' => 'value2' |
---|
| 76 | ); |
---|
| 77 | |
---|
| 78 | OR: |
---|
| 79 | |
---|
| 80 | $array = array |
---|
| 81 | ( |
---|
| 82 | 'var' => 'value', |
---|
| 83 | 'var2' => 'value2' |
---|
| 84 | ); |
---|
| 85 | |
---|
| 86 | Note that spaces are preferred around the '=>'. This is because only tabs |
---|
| 87 | on the left side are guaranteed to line up correctly using different |
---|
| 88 | tabstops. |
---|
| 89 | |
---|
| 90 | 12) Use the long format for <?php. Do NOT use <?. |
---|
| 91 | |
---|
| 92 | 13) All code should start with 1 tab. Example: |
---|
| 93 | |
---|
| 94 | <?php |
---|
| 95 | dosomething(); |
---|
| 96 | if ($a) |
---|
| 97 | { |
---|
| 98 | dosomemorestuff(); |
---|
| 99 | } |
---|
| 100 | |
---|
| 101 | NOT: |
---|
| 102 | |
---|
| 103 | <?php |
---|
| 104 | dosomething(); |
---|
| 105 | if ($a) |
---|
| 106 | { |
---|
| 107 | dosomemorestuff(); |
---|
| 108 | } |
---|
| 109 | |
---|
| 110 | 14) Use lower case for variable and function names. No stubbly-case (mixed-case) code. |
---|
| 111 | |
---|
| 112 | 15) (int)$var is preferred vs. intval($var). Also, is_int()/is_string()/is_array() |
---|
| 113 | should be used instead of gettype() where possible. |
---|
| 114 | |
---|
| 115 | 16) str_ functions should be used instead of ereg_ for simple text replacement, etc. |
---|
| 116 | For example, ereg_replace(';','',$string) is much slower than str_replace(';','',$string. |
---|
| 117 | Of course, for complicated regular expressions, you may still need ereg_. |
---|
| 118 | |
---|
| 119 | 17) Use the api function, copyobj($oldobject,$newobject), instead of |
---|
| 120 | $newobject = $oldobject. This is for performance when using php5. |
---|
| 121 | |
---|
| 122 | 18) Try to avoid creating new objects when the api-created ones will work. |
---|
| 123 | This is a performance issue. You might also be able to use copyobj() and then |
---|
| 124 | call the constructor of the class if another version of the object exists |
---|
| 125 | already. |
---|
| 126 | |
---|
| 127 | 19) Do not use, e.g., global $var; unless absolutely necessary. Please |
---|
| 128 | consider developing with register_globals=off to understand why. |
---|
| 129 | |
---|
| 130 | 20) Thanks for following these rules :) |
---|