02 - Control Structures
Mambo Coding Standards
Mar 03, 2007 at 10:37 AM

These include if, for, while, switch, etc.
Use of the "elseif" construct is permitted but highly discouraged in favor of the "else if" combination.

Here is an example if statement, since it is the most complicated of them:
<?php
if ((condition1) || (condition2)) {
    action1;
} elseif ((condition3) && (condition4)) {     action2; } else {     if (condition5) {         action3;     }
    defaultaction; } ?>

Control statements should have one space between the control keyword and opening parenthesis, to distinguish them from function calls.

The opening brace is written on the same line as the conditional statement. The closing brace is always written on its own line. Any content within the braces should be indented.

PHP allows for these statements to be written without braces in some circumstances. However, all "if", "elseif" or "else" statements should use braces.

You are strongly encouraged to always use curly braces even in situations where they are technically optional. Having them increases readability and decreases the likelihood of logic errors being introduced when new lines are added.

For switch statements:

Control statements written with the "switch" construct should have a single space before the opening parenthesis of the conditional statement, and also a single space after the closing parenthesis.

All content within the "switch" statement should be indented. Content under each "case" statement should contain an additional indent.

<?php
switch ($var) {
    case 1:
        break;
    case 2:
        break;
    default:
        break;
}
?>
<Previous   Next>

The Mambo Foundation is a volunteer non-profit organization and relies entirely upon the financial and human resource donations of the wider Mambo open source community to keep Mambo going.

We need your support!
Please make a donation today.