My PEAR: The Beginning - PEAR Coding Standards
(Page 3 of 4 )
While there is no commonly agreed way of writing code, the creators of PEAR have created their own way of writing code known as PCS or PEAR Coding Standards. In general you don't have to follow these standards, but if you ever want to submit your work to PEAR, you will have to follow them. If you visit the PEAR website you will find a lot of information about PEAR Coding Standards so I will not be going into great detail here.
To start with, all code should be indented correctly according to its context by using a suitable number of spaces. PEAR dictates four spaces. Also, if a parenthesis is optional but its inclusion improves clarity, then its inclusion is required. Comments are encouraged wherever possible. Avoid using # when commenting; instead, use the "//" for single line comments or the "/*" and "*/" for multi-line comments.
PEAR also dictates that all starting and finishing blocks of code are done like so: starting "<?php" and finishing "?>". This is mostly because some servers may throw an error if you use "<?" for the starting block. This requirement ensures that your code is more platform independent.
As for control structures, the curly braces should always be used. Control structures may include:
PEAR dictates that curly braces should be used even if there is only a single line in the block. The PHP interpreter is very lax in this regard. This is mainly for improving clarity.
Here is an example of PEAR compliant commenting:
<?php
//now check if the values in the two variables match
if ($a==$b) {
echo "the values match";
}else{
echo "The values do not match.";
}
/*In this block of code we evaluated the two variables to see if they matched.
Appropriate messages where printed out to show the outcome
*/
?>
The above example shows how to make single and multi-line comments that are PEAR compliant.
Next: Functions >>
More PEAR Articles Articles
More By David Web