04 - Function Definitions
Mambo Coding Standards
Mar 03, 2007 at 10:39 AM

Function declarations follow the "one true brace" convention:

<?php
function fooFunction( $arg1, $arg2 = '' ){
 
    if (condition) {
        statement;
    }
    return $val;
}
?>

Arguments with default values go at the end of the argument list. Always attempt to return a meaningful value from a function if one is appropriate.
Here is a slightly longer example:

<?php
function connect( &$dsn, $persistent = false ){
 
    if (is_array( $dsn )) {
        $dsninfo = &$dsn;
    } else {
        $dsninfo = DB::parseDSN( $dsn );
    }    if (!$dsninfo || !$dsninfo['phptype']) {
        return $this->raiseError();
    }    return true;
}
?>

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

<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.