home support FAQ resources services partners contact us contact us
 Web Application Previous  Next  
 Tutorial

So, this was almost a scientific approach to explain why formatting source code is
useful. Is there another reason? Oh, yes:Well-formatted source code just looks good.
Following are a few guidelines for what we think is the optimal style to use in
formatting source code. Please note that these are not mandated, but are regarded as
common style. Many industrial and Open Source projects have been formatted this
way, and it often pays to write in this style.
n Put all block tags (<?, ?>, <?php, <%, %>, {, }, etc.) on separate lines.
n Indent all blocks with tabs (ideally, set the tab width to no less than 4).
n Leave spaces between keywords and key characters, especially when doing
calculations.

n Group logical chunks of code within a block by placing them on consecutive
lines, and leave a blank line between all others.

n Separate blocks from each other using a blank line.

n Separate function headers and eventual function footers from the rest of the code

using a blank line (importing globals is treated as a part of the function header).

n Integrate block comments into the code, using the same indentation as the code

block to which each refers.

n Put all line comments into the same column throughout a block.

11 Coding Conventions

As an example, Listing 1.2 shows the earlier code snippet, reformatted.

Listing 1.2 Reformatted code snippet.

<?
function myfunc($myvar)
{
$somevar = $myvar * 2;
return($somevar + 1);
}
print(myfunc(1));
?>
Previous  Next  
Link Partners: Asia florist, Flowers to India, Hong kong flowers, Site submit, Cheap web hosting, China florist, Japan florist